[英]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.