选择带有触发器的所有复选框 - jquery

时间:2017-08-11 10:16:35

标签: javascript php jquery onclick eventtrigger

在我的SelectAll复选框上,单击触发器应该适用于所有输入 - 其上有点击事件。

触发selectall的工作,但取消选中selectall无效。它没有取消选中所有输入

<input type="checkbox" id="SelectAll" />

<input class="checkBoxClass" name="pid[]" id="pid_1" value="1" onclick="javascript: total_select(1,0.35,2700.00);" type="checkbox">

<input class="checkBoxClass" name="pid[]" id="pid_2" value="2" onclick="javascript: total_select(2,0.35,2700.00);" type="checkbox">

<input class="checkBoxClass" name="pid[]" id="pid_3" value="3" onclick="javascript: total_select(3,0.35,2700.00);" type="checkbox">

在我所做的之下

$(document).ready(function () {
    $("#SelectAll").click(function () {
      $(".checkBoxClass").attr('checked', $(this).attr('checked'));
        $(".checkBoxClass").change(function(){
            if (!$(this).attr("checked")){
                $("#SelectAll").attr("checked",false);
            }
        });
    });
});

以上Jquery为select-all / UN选择所有复选框 - 如果我删除下面的触发器,则有效

 $(document).ready(function () {
     $("#SelectAll").click(function(){
           $(".checkBoxClass").trigger("click"); 
           $(".checkBoxClass").attr('checked', true);    // if i remove this line then selectall for checkbox don't works            
     }); 
 }); 

但我需要在jquery之上触发 - total_select函数

function total_select(id,carat,price){

  var lenChkBox = $("input[name='pid[]']:checked").length;

      if(document.getElementById("pid_"+id).checked==true) {

         total_select.s++;                                  
         document.getElementById("noofs").innerHTML=total_select.s;

          total_select.c +=carat;
          var cs = (total_select.c).toFixed(2);
          document.getElementById("carat").innerHTML=cs;

          total_select.p +=price * carat;
          avg_price_per = total_select.p/total_select.c;
          var ca = (avg_price_per).toFixed(2);

          document.getElementById("price").innerHTML="$"+ca;

    } else {
            total_select.s--;
           document.getElementById("noofs").innerHTML=total_select.s;

            total_select.c -=carat;
            cs = (total_select.c).toFixed(2);
            document.getElementById("carat").innerHTML=cs;

            total_select.p -=price * carat;
            avg_price_per = total_select.p/total_select.c;
            var ca = (avg_price_per).toFixed(2);
            document.getElementById("price").innerHTML="$"+ca;
}

我尝试了很多:

   $(".checkBoxClass").attr('checked', true).triggerHandler('click');
   $(".checkBoxClass").prop('checked', true).triggerHandler('click');

但仍然无效,它会检查&#39; SelectAll&#39;并触发total_select功能并获得适当的结果,但当我取消选择/取消选中&#39; SelectAll&#39;我的输入都没有取消选择/取消选中

2 个答案:

答案 0 :(得分:0)

我发现答案如下,但仍有任何人有任何其他最佳解决方案让我知道,谢谢

 $(document).ready(function () {
     $("#SelectAll").click(function(){
           $(".checkBoxClass").trigger("click"); 
           $(".checkBoxClass").attr('checked', true);  
           $(".checkBoxClass").attr('checked', $(this).attr('checked')); //add this line and working proper - hope will help someone
     }); 
 }); 

答案 1 :(得分:0)

我知道它不是活动线程,但我已经卖出了一个类似的问题,我想提出解决方案:D

通过此操作,将仅选中或取消选中那些复选框,以及(因此触发了)与checked具有不同的#SelectAll属性的复选框。

$(function () {
    $('#SelectAll').click(function () {
        $('.checkBoxClass').each(function () {
            if ($(this).prop('checked') !== $('#SelectAll').prop('checked')) {
                $(this).trigger('click');
            }
        });
    });
});

function total_select(id,carat,price){
  alert(id);
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

ALL
<input type="checkbox" id="SelectAll" />
A
<input class="checkBoxClass" name="pid[]" id="pid_1" value="1" onclick="javascript: total_select(1,0.35,2700.00);" type="checkbox">
B
<input class="checkBoxClass" name="pid[]" id="pid_2" value="2" onclick="javascript: total_select(2,0.35,2700.00);" type="checkbox">
C
<input class="checkBoxClass" name="pid[]" id="pid_3" value="3" onclick="javascript: total_select(3,0.35,2700.00);" type="checkbox">

<div id="result">
</div>

相关问题