本文介绍了jQuery-至少选中一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习JQuery,这主要归功于Stack Overflow上的积极参考.我需要一个函数来检查元素中具有相同CSS类的所有复选框.如果至少检查其中之一,则应返回true.元素中还有其他与检查无关的框.

I am in the process of learning JQuery thanks mostly to the positive reference here on Stack Overflow. I need a function that checks all the checkboxes in an element which have the same CSS class. It should returns true if at least one of them is checked. There are also other boxes in the element that are irrelevant to the check.

CSS类不是必需的,只能用于创建一种识别组中复选框的方法.感觉这是一种不好的做法,因此欢迎您提出其他有关识别它们的方法的建议.

The CSS class is unnecessary and only in place to create a way to identify the checkboxes in the group. It feels like bad practice, so any recommendations about other ways to identify them are welcome.

推荐答案

由于页面布局,不能选择单选按钮列表.我使用自定义验证程序解决了该问题.

A radio button list was not an option due to the page layout. I solved it with a Custom Validator.

将此JavaScript函数添加到页面

Add this JavaScript function to the page

function ValidateDDA(s, a) {
    a.IsValid = ($(".chk:checked", $('parentDiv')).length > 0);
}

然后将CustomValidator添加到页面

Then add a CustomValidator to the page

<asp:CustomValidator runat="server" ID="cvDDLCorrect" ClientValidationFunction="ValidateDDA"
ErrorMessage="No hay resuesta correcta" CssClass="error" />

像魅力一样工作.感谢您的建议.

Works like a charm. Thanks for suggestions.

这篇关于jQuery-至少选中一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 02:48