在jquery的实时事件中切换问题

时间:2010-01-30 07:21:16

标签: jquery livequery

我坚持这个可以任何人帮助我...

这是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"); });
        });
    });

第一次点击后它只切换'锚文本不切换下拉列表和文本框..

2 个答案:

答案 0 :(得分: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 :(得分: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");
        }
    });
});