doudengshen5591 2012-10-22 01:13
浏览 65
已采纳

使用jQuery Selector指定多个对象

I have a navigation bar which consists of php-generated html as follows:

<ul>
<div class="container"><button type="button" name="1" class="delButton" alt="Delete"></button></div>
<a href="/" alt="Home">Home</a>
<div class="container"><button type="button" name="2" class="delButton" alt="Delete"></button></div>
<a href="/" alt="Events">Events</a>
<div class="container"><button type="button" name="3" class="delButton" alt="Delete"></button></div>
<a href="/" alt="Calendar">Calendar</a>
<div class="container"><button type="button" name="4" class="delButton" alt="Delete"></button></div>
<a href="/" alt="About Us">About Us</a>
</ul>

Each set consists of a delete button and a link. The name attribute of the button contains the unique id of the link following it, as per the mySQL database.

I'm using the following jQuery code to remove the link when its corresponding delete button is clicked. Note that I'm using a custom ajax() method, and not jQuery's ajax method.

$('.container button').click(function() {
     if (confirm('Are you sure you wish to remove this link?'))
        ajax('POST', '/deletelink.php', 'id='+this.name, function(xmlhttp) {
           $('.nav ul').load('/getnav.php');
        });
});

The above code works as expected when I click any one of the delete buttons, and it removes the corresponding link. However, if after deleting one link I try clicking on another delete button, nothing happens. The confirm() alert box doesn't even show up. This suggests that the click() function is not being entered for the second delete attempt.

My interpretation of this is that once the function executes the first time, it resolves the selector to that specific button that was clicked; meaning that subsequently clicking on a different '.container button' is going to have no effect in terms of this function.

Is my assessment correct? If so, is there any way to prevent the function from resolving the selector to the specific object? If not, are there any alternatives that allow me to use one function for all of the delete buttons I have setup now?

  • 写回答

3条回答 默认 最新

  • dongshuofu0039 2012-10-22 01:16
    关注

    Your click handlers are overwritten when you load the getnav.php again. You can use the following construct:

    $(document).on('click', '.container button', function() {
        // click handler code here
    }
    

    You should be able to narrow down the selectors as well:

    $('.nav ul').on('click', '.container button', function() {
        // click handler code here
    }
    

    This binds the click handler on the <ul> itself and then uses the .container button selector to determine where to "send" the click event to.

    See also: .on()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line