Get checkbox value in jQuery
我如何获得jQuery中的复选框的值?
To get the value of the Value attribute you can do something like this:
$("input[type='checkbox']").val();
Or if you have set a class
or id
for it, you can:
$('#check_id').val();
$('.check_class').val();
However this will return the same value whether it is checked or not, this can be confusing as it is differnt to the submitted form behaviour.
To check whether it is checked or not, do:
if ($('#check_id').is(":checked"))
{
// it is checked
}
Those 2 ways are working:
$('#checkbox').prop('checked')
$('#checkbox').is(':checked')
(thanks @mgsloan) $('#test').click(function() {
alert("Checkbox state (method 1) = " + $('#test').prop('checked'));
alert("Checkbox state (method 2) = " + $('#test').is(':checked'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Check me: <input id="test" type="checkbox" />
Try this small solution:
$("#some_id").attr("checked") ? 1 : 0;
or
$("#some_id").attr("checked") || 0;
链接地址: http://www.djcxy.com/p/9580.html
上一篇: 测试复选框是否使用jQuery进行检查
下一篇: 获取jQuery中的复选框值