简体   繁体   English

使用jquery动态添加的复选框(当用户单击“全选”复选框时显示选中状态)

[英]Dynamically added checkbox using jquery (show checked when user clicks on Select All checkbox)

When page loads, there are 10 checkboxes. 页面加载时,有10个复选框。 When I click on Select all check box all 10 check boxes gets selected. 当我单击Select all选复选框时,将选中所有10个复选框。 Now I add 10 more records dynamically (using ajax) having checkboxes (Total 20 check boxes on page). 现在,我动态地(使用ajax)添加了10个具有复选框的记录(使用页面上的20个复选框)。 Now I click on Select All check box, Original checkboxes ie first 10 check boxes gets unchecked (newly added 10 are as it is ie unchecked). 现在,我单击Select All复选框,原始复选框,即前10个复选框被取消选中(新添加的10个即被取消选中)。 Again I click on Select All check box, at this time all 20 check boxes should get selected but nothing is getting selected. 再次单击“全Select All复选框,这时应选中所有20个复选框,但未选中任何复选框。

$(".productListed").attr( "checked", true ); and $(".productListed").attr( "checked", false ); $(".productListed").attr( "checked", false );

$(document).on('click', "#selectall", function(){ // my code });

I have gone through Event binding on dynamically created elements? 我已经对动态创建的元素进行了事件绑定?

How to set all dynamically added checkbox to checked/unchecked when I click on Select All ? 当我单击Select All时,如何将所有动态添加的复选框设置为选中/未选中?

I don't think this is the case of event binding of dynamically created elements. 我认为这不是动态创建的元素的事件绑定的情况。 I have created a working example - see http://jsfiddle.net/qwt7s0a7/ 我创建了一个工作示例-请参阅http://jsfiddle.net/qwt7s0a7/

HTML 的HTML

<div class="container">
    <input class="cb" type="checkbox"/>
</div>
<button id='add'>Add checkbox</button>
<button id='selectAll'>Select all</button>

js js

$(function(){
    $('#add').click(function(){
        $('.container').append('<input class="cb" type="checkbox"/>');
    });
    $('#selectAll').click(function(){
        var checked = $('.container .cb').first().prop('checked');
        $('.container .cb').prop('checked', !checked)
    });
});

Please find the sample code below and example at: https://jsfiddle.net/vv48fh3t/ 请在下面找到示例代码和示例: https : //jsfiddle.net/vv48fh3t/

Html HTML

<input type="button" id="btnCheckAll" value="Check All"/>
<input type="button" id="btnAddNew" value="Add checkbox" >
<div class="checkDiv">
    <input type="checkbox" class="chk"/>check 1
    <input type="checkbox" class="chk"/>check 2
    <input type="checkbox" class="chk"/>check 3
    <input type="checkbox" class="chk"/>check 4
</div>

JS JS

$(document).ready(function(){
    $("#btnCheckAll").click(function(){
    $(".chk").each(function(){
    $(this).prop("checked","checked");
    });
  });

  $("#btnAddNew").click(function(){
     for(var i=0;i<5;i++){
     $(".checkDiv").append("<input type='checkbox' class='chk'/>check "+i);
     }
 });
});

I Used .prop("checked", true) instead of attr( "checked", true ) 我用.prop("checked", true)代替了attr( "checked", true )

$(".productListed").prop( "checked", true ); and $(".productListed").prop( "checked", false );

Also use this whenever you want to read any dynamically added element on HTML document page. 每当您想读取HTML文档页面上任何动态添加的元素时,也请使用此功能。

$(document).on('click', "#selectall", function(){ // my code });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM