Getting checkbox checked or not in a array in jquery
This question already has an answer here:
The value
attribute return value of input. You should use checked
that return status of checkbox.
var is_checked = $("input[name='mark_box[]']").map(function(){
return this.checked ? 1 : 0;
}).get();
var is_checked = $("input[name='mark_box[]']").map(function(){
return this.checked ? 1 : 0;
}).get();
console.log(is_checked);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" checked id="mark_box0" name="mark_box[]">
<input type="checkbox" id="mark_box1" name="mark_box[]">
<input type="checkbox" id="mark_box2" name="mark_box[]">
The way you are using is correct but you are getting the value which will remain same at every state. You have to check that the checkbox is checked or not. Here is the code:
var is_checked = $("input[name='mark_box[]']").map(function(){
return $(this).attr("checked");
}).get();
Let me know if you have any query.
你需要检查checked
而不是value
$(".button").click(function() {
var checkedArray = $("input[name='mark_box[]']").map(function() {
return this.checked;
}).get();
console.log(checkedArray);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="mark_box0" name="mark_box[]">
<input type="checkbox" id="mark_box1" name="mark_box[]">
<input type="checkbox" id="mark_box2" name="mark_box[]">
<button class='button'>Submit</button>
链接地址: http://www.djcxy.com/p/9536.html
上一篇: 检查复选框选中JavaScript不工作
下一篇: 获取复选框选中或不在jQuery中的数组