简体   繁体   English

如何根据已选中或未选中复选框添加/删除项目?

[英]How to add/remove item on the basis of checkbox checked or unchecked?

I have written below lines of code 我写了下面的代码行

 var selectedFinal = []; //On click check all records $(document).on("click", "#completebatch,.commoncheckbox", function() { var checkBoxCount = 0; var selected = []; $("#selected_students").empty(); $('#studentListBody input:checked').not(".disabled-check").each(function() { var id = $(this).val(); if ($("#" + id).is(":checked")) { checkBoxCount++; selected.push(id); } else { checkBoxCount--; selected.pop(id); } }); $("#allStudentIds").val($.unique(selected)); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <thead> <th><input onclick="checkenabledisable()" id="completebatch" type="checkbox"></th> </thead> <tbody> <tr> <td><input class="commoncheckbox disabled-check" value="578" type="checkbox" disabled=""></td> <td>abc</td> </tr> <tr> <td><input class="commoncheckbox" value="357" type="checkbox"></td> <td>abc</td> </tr> <tr> <td><input class="commoncheckbox" value="123" type="checkbox"></td> <td>abc</td> </tr> </tbody> </table> 

I have created html table with checkboxes, where user can uncheck or check the checkboxes. 我创建了带有复选框的html表,用户可以在其中取消选中或选中复选框。 Based on checked or unchecked checkboxes I am trying to push the id's (value) of the checked checkboxes in the "selected" array. 基于选中或未选中的复选框,我试图将选中的复选框的ID(值)推送到“选定”数组中。 The above code is not adding the value of that checkbox. 上面的代码未添加该复选框的值。 Also When I try to uncheck the checkbox that id is not removed from the "selected" array. 另外,当我尝试取消选中未从“选定”数组中删除ID的复选框时。 Please help!!! 请帮忙!!!

Why don't simply use map() and get() on the checked check boxes: 为什么不简单在checked复选框上使用map()get()

 var selectedFinal = []; //On click check all records $(document).on("click", "#completebatch,.commoncheckbox", function () { selectedFinal = $('.commoncheckbox:checked').map((i,el) =>{ return $(el).attr('value') }).get(); console.log(selectedFinal); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <thead> <th><input onclick="checkenabledisable()" id="completebatch" type="checkbox"></th> </thead> <tbody> <tr> <td><input class="commoncheckbox disabled-check" value="578" type="checkbox" disabled=""></td> <td>abc</td> </tr> <tr> <td><input class="commoncheckbox" value="357" type="checkbox"></td> <td>abc</td> </tr> <tr> <td><input class="commoncheckbox" value="123" type="checkbox"></td> <td>abc</td> </tr> </tbody> </table> 

Please try executing the following script: 请尝试执行以下脚本:

 <tbody>
<tr>
  <td><input class="commoncheckbox disabled-check"  id = "checkbox1" value="578" type="checkbox" onclick="check(this.id);" disabled=""></td>
 <td>abc</td>
</tr>
<tr>
  <td><input class="commoncheckbox" value="357"  id = "checkbox2" onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
<tr>

  <td><input class="commoncheckbox" value="123" id = "checkbox3"  onclick="check(this.id);" type="checkbox"></td>
  <td>abc</td>
</tr>
    </tbody>
  </table>



<script>
selected = [];
function check(a){
    if(document.getElementById(a).checked = true){
        selected.push(document.getElementById(a).value); 
    }

}

I fix as well your code on this jsfiddle: 我也在此jsfiddle上修复了您的代码:

http://jsfiddle.net/xpvt214o/792746/ http://jsfiddle.net/xpvt214o/792746/

// find elements
var selectedFinal = [];
$( document ).ready(function() {
             //On click check all records

                var checkBoxCount = 0;
                var selected = [];


                $('input:checkbox.commoncheckbox').not(".disabled-check").change(function (event) {

                    var target = event.target;
                    if ($(target).is(":checked"))
                    {
                        checkBoxCount++;
                        selected.push(target.value);
                    } 
                    else
                    {
                        checkBoxCount--;
                        selected.pop(target.value);
                    }
                    console.log('selected', selected)
                });

                $("#allStudentIds").val($.unique(selected));

           });

暂无
暂无

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

相关问题 如何在html表中未选中的复选框的基础上添加和删除ID? - How to append and remove ids on the basis of checkbox checked unchecked in html table? 如何向 state 添加/删除数据,具体取决于复选框是否已选中然后未选中 - how to add/remove data to state depending on if the checkbox was checked and then unchecked 如果选中复选框并且未选中复选框而不是删除,如何添加名称属性 - How to add name attribute if checkbox is checked and if it is unchecked than remove 如何知道如果选中复选框则将元素添加到数组,如果未选中则将其删除? - How to know add element to an array if checkbox is checked and remove it if unchecked? 取消选中/选中复选框时,javascript在div中添加/删除文本 - javascript add/remove text in div when checkbox is unchecked/checked 选中和取消选中时,将复选框值添加到数组中/从数组中删除复选框值(jQuery) - Add/remove checkbox values to/from array when checked and unchecked (jQuery) 检查复选框是否被选中增加价值,否则被取消选择价值 - check if checkbox is checked add value else if unchecked remove value 如果选中复选框,如何调用函数,如果未选中则从附加列表中删除 - How to call a function if checkbox checked and remove from appended list if unchecked 如果所有子复选框都未选中,如何删除选中的复选框? - How to remove checked checkbox if all sub check boxes are unchecked? 取消选中复选框时删除项目 - Remove item when unchecked checkbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM