简体   繁体   English

如何检查页面上的所有复选框是否都已选中?

[英]How do I check if any checkboxes at all on the page are checked?

I need to check, using jQuery, if any checkboxes on the page are checked. 我需要使用jQuery检查页面上是否有任何复选框。 The particular checkbox set they are contained in doesn't matter, just any of them. 它们所包含的特定复选框集无关紧要,只是其中任何一个都没有关系。

Any ideas? 有任何想法吗?

Thanks! 谢谢!

var isChecked = $("input[type=checkbox]").is(":checked");

要么

var isChecked = $('input:checkbox').is(':checked')

The following function returns true if there are any ticked checkboxes and false otherwise. 如果选中任何复选框,则以下函数返回true否则返回false

function anyCheckbox()
{
    var inputElements = document.getElementsByTagName("input");
    for (var i = 0; i < inputElements.length; i++)
        if (inputElements[i].type == "checkbox")
            if (inputElements[i].checked)
                return true;
    return false;
}
if($('checkbox:checked').length > 0)
    //Some checkbox is checked

This should find any checkboxes on the page that are checked 这应该在页面上找到所有选中的复选框

function checkbox(){
    var ischecked =$('input[type=checkbox]:checked').length;
    if (ischecked <= 0){        // you can change your condition
    alert("No Record Selected");
    return false;
    }else
    {
      -------//your Action that you want
    }

}

if you only want to look at checkboxes, not radio buttons: var isChecked = $("input:checkbox").is(":checked"); 如果只想查看复选框,而不是单选按钮:var isChecked = $(“ input:checkbox”)。is(“:checked”);

if you're ok with checkboxes or radio buttons: var isChecked = $("input").is(":checked"); 如果您可以使用复选框或单选按钮,则可以:var isChecked = $(“ input”)。is(“:checked”);

This works: 这有效:

var ischecked =$('input[type=checkbox]:checked').length;

so 所以

if (ischecked > 0)

then at least one is checked. 然后至少检查一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM