Javascript 检查复选框是否被 jQuery 选中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2204250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:14:01  来源:igfitidea点击:

Check if checkbox is checked with jQuery

javascriptjqueryhtmlcheckbox

提问by Jake

How can I check if a checkbox in a checkbox array is checked using the id of the checkbox array?

如何使用复选框数组的 id 检查复选框数组中的复选框是否被选中?

I am using the following code, but it always returns the count of checked checkboxes regardless of id.

我正在使用以下代码,但它始终返回选中复选框的计数,而不管 id。

function isCheckedById(id) {
    alert(id);
    var checked = $("input[@id=" + id + "]:checked").length;
    alert(checked);

    if (checked == 0) {
        return false;
    } else {
        return true;
    }
}

采纳答案by nickf

IDs must be unique in your document, meaning that you shouldn'tdo this:

ID 在您的文档中必须是唯一的,这意味着您不应该这样做:

<input type="checkbox" name="chk[]" id="chk[]" value="Apples" />
<input type="checkbox" name="chk[]" id="chk[]" value="Bananas" />

Instead, drop the ID, and then select them by name, or by a containing element:

相反,删除 ID,然后按名称或包含元素选择它们:

<fieldset id="checkArray">
    <input type="checkbox" name="chk[]" value="Apples" />

    <input type="checkbox" name="chk[]" value="Bananas" />
</fieldset>

And now the jQuery:

现在是 jQuery:

var atLeastOneIsChecked = $('#checkArray:checkbox:checked').length > 0;
//there should be no space between identifier and selector

// or, without the container:

var atLeastOneIsChecked = $('input[name="chk[]"]:checked').length > 0;

回答by John Boker

$('#' + id).is(":checked")

That gets if the checkbox is checked.

如果复选框被选中,那会得到。

For an array of checkboxes with the same name you can get the list of checked ones by:

对于具有相同名称的复选框数组,您可以通过以下方式获取选中的列表:

var $boxes = $('input[name=thename]:checked');

Then to loop through them and see what's checked you can do:

然后遍历它们并查看检查的内容,您可以执行以下操作:

$boxes.each(function(){
    // Do stuff here with this
});

To find how many are checked you can do:

要查找检查了多少,您可以执行以下操作:

$boxes.length;

回答by Prasanna Rotti

$('#checkbox').is(':checked'); 

The above code returns true if the checkbox is checked or false if not.

如果复选框被选中,上面的代码返回真,否则返回假。

回答by justnajm

All following methods are useful:

以下所有方法都很有用:

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

$('#checkbox').prop('checked')

$('#checkbox')[0].checked

$('#checkbox').get(0).checked

It is recommended that DOMelement or inline "this.checked" should be avoided instead jQuery on method should be used event listener.

建议应避免使用 DOMelement 或内联“this.checked”,而应使用 jQuery on 方法进行事件侦听器。

回答by Kundan roy

jQuery code to check whether the checkbox is checked or not:

检查复选框是否被选中的jQuery代码:

if($('input[name="checkBoxName"]').is(':checked'))
{
  // checked
}else
{
 // unchecked
}

Alternatively:

或者:

if($('input[name="checkBoxName"]:checked'))
{
    // checked
}else{
  // unchecked
}

回答by Techie

The most important concept to remember about the checked attribute is that it does not correspond to the checked property. The attribute actually corresponds to the defaultChecked property and should be used only to set the initial value of the checkbox. The checked attribute value does not change with the state of the checkbox, while the checked property does. Therefore, the cross-browser-compatible way to determine if a checkbox is checked is to use the property

关于checked 属性要记住的最重要的概念是它不对应于checked 属性。该属性实际上对应于 defaultChecked 属性,应该仅用于设置复选框的初始值。选中的属性值不会随着复选框的状态而改变,而选中的属性会。因此,确定复选框是否被选中的跨浏览器兼容的方法是使用属性

All below methods are possible

以下所有方法都是可能的

elem.checked 

$(elem).prop("checked") 

$(elem).is(":checked") 

回答by Mohammed Shaheen MK

You can use this code,

您可以使用此代码,

if($("#checkboxId").is(':checked')){
     // Code in the case checkbox is checked.
} else {
     // Code in the case checkbox is NOT checked.
}

回答by Subodh Ghulaxe

As per the jQuery documentationthere are following ways to check if a checkbox is checked or not. Lets consider a checkbox for example (Check Working jsfiddlewith all examples)

根据 jQuery文档,有以下方法可以检查复选框是否被选中。例如,让我们考虑一个复选框(检查所有示例的工作jsfiddle

<input type="checkbox" name="mycheckbox" id="mycheckbox" />
<br><br>
<input type="button" id="test-with-checked" value="Test with checked" />
<input type="button" id="test-with-is" value="Test with is" />
<input type="button" id="test-with-prop" value="Test with prop" />

Example 1 - With checked

示例 1 - 选中

$("#test-with-checked").on("click", function(){
    if(mycheckbox.checked) {
        alert("Checkbox is checked.");
    } else {
        alert("Checkbox is unchecked.");
    }
}); 

Example 2 - With jQuery is, NOTE - :checked

示例 2 - 使用 jQuery,注意 - :checked

var check;
$("#test-with-is").on("click", function(){
    check = $("#mycheckbox").is(":checked");
    if(check) {
        alert("Checkbox is checked.");
    } else {
        alert("Checkbox is unchecked.");
    }
}); 

Example 3 - With jQuery prop

示例 3 - 使用 jQuery 道具

var check;
$("#test-with-prop").on("click", function(){
    check = $("#mycheckbox").prop("checked");
    if(check) {
         alert("Checkbox is checked.");
    } else {
        alert("Checkbox is unchecked.");
    }
}); 

Check Working jsfiddle

检查工作jsfiddle

回答by Vishnu Sharma

You can try this:

你可以试试这个:

<script>
function checkAllCheckBox(value)
{
   if($('#select_all_').is(':checked')){
   $(".check_").attr ( "checked" ,"checked" );
    }
    else
    {
        $(".check_").removeAttr('checked');
    }

 }

</script>
<input type="checkbox" name="chkbox" id="select_all_" value="1" />


<input type="checkbox" name="chkbox" class="check_" value="Apples" />
<input type="checkbox" name="chkbox" class="check_" value="Bananas" />
<input type="checkbox" name="chkbox" class="check_" value="Apples" />
<input type="checkbox" name="chkbox" class="check_" value="Bananas" />

回答by endur

You can use any of the following recommended codes by jquery.

您可以使用 jquery 推荐的以下任何代码。

if ( elem.checked ) {};
if ( $( elem ).prop( "checked" ) ) {};
if ( $( elem ).is( ":checked" ) ) {};