2010-01-30 65 views
3

我堅持這個任何人都可以幫助我的現場活動......切換問題jQuery中

下面是HTML

<tr class="appendvalue"> 
    <td colspan="2"> 
    <asp:DropDownList ID="ddlSource" runat="server"></asp:DropDownList> 
    <asp:TextBox ID="txtSourceValue" runat="server" CssClass="hide" /> 
    <a href="#" class="t12">add static value</a> 
    </td> 

這裏是jQuery的

$(document).ready(function() { 
     $('.appendvalue > td > a').live("click", function(e) { 
      e.preventDefault(); 
      $(this).prev().prev().toggle(); 
      $(this).prev().toggle(); 
      $(this).toggle(function() { $(this).text("select from dropdown"); }, function() { $(this).text("add static value"); }); 
     }); 
    }); 

第一次點擊後,只切換'錨點文本不切換下拉菜單和文本框..

回答

1

.toggle()是真的click事件的便捷方法,因此使用.toggle()相同的元素的單擊事件處理程序內將是有問題的。相反...

$(document).ready(function() { 
    $('.appendvalue > td > a').live("click", function(e) { 
     e.preventDefault(); 
     $(this).prevAll().toggle(); 
     if ($(this).parent().find("input").is(":hidden")) { 
      $(this).text("add static value"); 
     } else { 
      $(this).text("select from dropdown"); 
     } 
    }); 
}); 
2

試試這個:

$(document).ready(function() { 
    $('.appendvalue > td > a').live("click", function(e) { 
     e.preventDefault(); 
     var $this = $(this); 
     $this.prevAll().toggle(); 
     $this.toggle(function() { $this.text("select from dropdown"); }, function() { $this.text("add static value"); }); 
    }); 
}); 
+1

如果您打算使用$(this)多於一次緩存它。 var $ this = $(this)。然後使用$ this剩下的地方,意味着你不重新創建jQuery對象。 – PetersenDidIt 2010-01-30 15:42:09

+0

@petersendidit已經很晚了。我聲稱豁免。 :)良好的捕獲,不知道爲什麼我沒有看到,當我編碼它。它已被修復。謝了哥們! – 2010-01-30 21:22:55