检查是否使用jQuery选择了具有相似ID的所有元素

时间:2015-06-05 08:30:41

标签: jquery

我想检查是否选择了以id approve-开头的所有按钮。按钮是动态创建的,按钮数量不是恒定的。

<button type="button" id="approve-<?php echo $item->getId();?>">Approve</button>

<button type="button" id="reject-<?php echo $item->getId();?>">Reject</button>

我知道一个函数$j("button[id^='approve-']",但是在代码提醒“All Selected”后,即使单击了其中一个按钮。

if($j("button[id^='approve-']")){
        alert("All Clicked");
      }

1 个答案:

答案 0 :(得分:2)

您可以添加单击该按钮的属性,然后点击批准按钮长度检查批准按钮长度

$(document).on('click', 'button', function () {
    $(this).attr('clicked', true);
    if ($("button[id^='approve-']").length == $("button[id^='approve-'][clicked]").length) {
        alert("all approved clicked")
    }
});

<强> DEMO