Get checkbox value in jQuery

2019-01-01 12:02发布

How can I get a checkbox's value in jQuery?

15条回答
与君花间醉酒
2楼-- · 2019-01-01 12:45

Just to clarify things:

$('#checkbox_ID').is(":checked")

Will return 'true' or 'false'

查看更多
路过你的时光
3楼-- · 2019-01-01 12:46

Try this small solution:

$("#some_id").attr("checked") ? 1 : 0;

or

$("#some_id").attr("checked") || 0;
查看更多
路过你的时光
4楼-- · 2019-01-01 12:47

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 different to the submitted form behaviour.

To check whether it is checked or not, do:

if ($('#check_id').is(":checked"))
{
  // it is checked
}
查看更多
登录 后发表回答