我正在使用JQuery。
我的问题是:如何更新CheckBox
数组变量中多个es的值?
我对其进行了测试,并在每次单击CheckBox
时更新了检查CheckBox
值,并CheckBox
在每次单击时从数组中删除了未检查值。到现在为止还挺好。
但是在我的场景中,我有5个以上的CheckBox
es,如果单击任何复选框,我想检查所有其他CheckBox
es并更新它们在数组变量中的值。
我该如何实现?
U可以创建2个数组:checked
并unchecked
对此进行处理
试试这个
$(document).ready(function(){
$('input[type=checkbox]').on('change', function() {
var checked = [];
var unchecked = [];
$("input[type='checkbox']:checked").each(function() {
checked[checked.length] = this.value;
});
$("input[type='checkbox']:not(:checked)").each(function() {
unchecked[unchecked.length] = this.value;
});
console.log(checked);
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" ></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
</head>
<body>
<input type="checkbox" value="1" />1
<input type="checkbox" value="2" />2
<input type="checkbox" value="3" />3
<input type="checkbox" value="4" />4
<input type="checkbox" value="5" />5
</body>
</html>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句