在jQuery中获取复选框值
问题描述:
如何在jQuery中获取复选框的值?
How can I get a checkbox's value in jQuery?
答
要获取Value属性的值,像这样:
To get the value of the Value attribute you can do something like this:
$("input[type='checkbox']").val();
或者如果您设置了 class
id
,您可以:
Or if you have set a class
or id
for it, you can:
$('#check_id').val();
$('.check_class').val();
但是,无论是否选中,都会返回 same 这可能会令人困惑,因为它与提交的表单行为不同。
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
}