简体   繁体   English

jQuery检查复选框是否在页面上

[英]jQuery check if Checkboxes are on page

I have a form that loads via ajax. 我有一个通过ajax加载的表单。 Depending on the parameters sent to fetch the form, the form may have no checkboxes or it may have one or more check boxes. 根据发送来获取表单的参数,该表单可能没有复选框,也可能有一个或多个复选框。

I want to check if a checkbox is present on the page and if yes prompt the user to select one during validation if not selected. 我想检查页面上是否存在一个复选框,如果是,则提示用户在验证过程中选择一个(如果未选中)。

If present, the checkboxes have the same class and a different ID. 如果存在,则复选框具有相同的类和不同的ID。

<input type="checkbox" name="associatedClass" 
value="$associatedClsNumVal" class="getAssociatedClass" 
id="$associatedClsNumVal" />

What is the best approach? 最好的方法是什么? Can jquery "sense" if there are checkboxes on a page/in a form? 如果页面/表单中有复选框,jquery可以“感知”吗?

Thanks. 谢谢。


Thanks for the help on my previous post. 感谢您对我以前的帖子的帮助。 I am adding to this one as it's the same type problem. 我将其添加到同一类型的问题中。

I now have a page where there are one or more groups of checkboxes and I need to ensure that only one checkbox is checked in each group. 我现在有一个页面,其中有一个或多个复选框组,我需要确保每个组中仅选中一个复选框。

Since I do not know how many groups there are until the page loads, what is the best approach to check this? 由于在页面加载之前,我不知道有多少个组,因此最好的检查方法是什么?

Thanks! 谢谢!

The easiest way is to check if there are any inputs of the type checkbox on the page. 最简单的方法是检查页面上是否有任何类型的复选框输入。 With jQuery you can use CSS selectors such as input[type="checkbox"] to find elements in the DOM. 使用jQuery,您可以使用CSS选择器(例如input[type="checkbox"]在DOM中查找元素。 Combine that with .length and you can get how many there are on the page. 将其与.length结合使用,您可以获取页面上的数量。 The complete jQuery code for this would be 完整的jQuery代码将是

if ( $('input[type="checkbox"]').length ) {
    // Validate checkboxes
}

If there could be other checkboxes on the page that you wouldn't want validated then you can limit your search to a particular element. 如果页面上可能还有其他不想验证的复选框,则可以将搜索范围限制为特定元素。 For example, if you only wanted to validate checkboxes within a specific form with an ID of myForm then you can select the form and use .find() to search for checkboxes within and only within that form. 例如,如果您只想验证ID为myForm的特定表单中的复选框,则可以选择该表单并使用.find()来搜索该表单中以及仅在该表单中的复选框。

if ( $('#myForm').find('input[type="checkbox"]').length )
{
    // Validate checkboxes
}

However with the validation you probably want to check each input individually and so instead of just checking the length to make sure there is at least one checkbox on the page we can use .each to find all checkboxes and then process them one at a time. 但是,通过验证,您可能希望单独检查每个输入,因此,不仅要检查长度以确保页面上至少有一个复选框,我们还可以使用.each查找所有复选框,然后一次处理一个复选框。

$('input[type="checkbox"]').each(function () {
    // Validate this checkbox
});

The advantage of that is if there are none on the page it won't try and validate anything and it's perfectly valid, so we don't need to check to see if there are any before our validation. 这样做的好处是,如果页面上没有任何内容,它将不会尝试验证任何东西,并且完全有效,因此在验证之前,我们无需检查是否有任何东西。

$(':checkbox').length

will get you a count of all checkboxes on the page. 将为您提供页面上所有复选框的计数。 You can narrow this down by giving them a common class if you need to be more specific. 如果需要更具体,可以通过给他们一个普通的班级来缩小范围。

For example, to just count the checkboxes with class 'section1': 例如,仅计算类“ section1”的复选框:

var $checks = $('.section1:checkbox');
console.log('There are ' + $checks.length + '  checkboxes');

https://jsbin.com/bowasi/edit?html,js,console,output https://jsbin.com/bowasi/edit?html,js,控制台,输出

if ( $( "#myElement" ).length ) {

   // Your code here

}

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

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