如何验证一组单选按钮? [英] How do validate a group of radio buttons?

查看:72
本文介绍了如何验证一组单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在线调查,正在对其进行jquery验证.

所以每个问题都有4个选项,其中每个选项都是一个单选按钮.

在构建我的jquery验证规则时,应如何引用它们?

每个问题选项都有一个ID和名称.

下面是到目前为止我正在做的,而且没有用.

$(document).ready(function() {

 $("#myForm").validate({
           rules : {     
               someQuestionId123 : {required : true }
           });
  });

其中someQuestionId123是单选按钮元素的名称.

解决方案

您使用}); ...

不正确地关闭了规则

rules : {     
    someQuestionId123 : {required : true }
});


使用jQuery Validate插件的.validate()方法,您必须通过name引用它们.

由于一组radio按钮应该共享相同的name,因此您已经正确地进行了操作(解决了括号中的问题之后)...

$(document).ready(function() {

    $("#myForm").validate({
        rules: {     
            someQuestionId123: { // <-- NAME attribute
                required: true 
            }
        }  // <-- right here, you incorrectly had '});'
    });
});

HTML :

<input type="radio" name="someQuestionId123" value="one" />
<input type="radio" name="someQuestionId123" value="two" />
<input type="radio" name="someQuestionId123" value="three" />
<input type="radio" name="someQuestionId123" value="four" />

演示: http://jsfiddle.net/5dsJQ/


最后,最新的jsFiddle被破坏了,因为name ...

中有特殊字符,例如-

rules: {     
    someQuestionId-123: { 
        required: true 
    }
}

根据参考文档" ,您需要将该名称括在引号中以解决此问题. ..

rules: {     
    "someQuestionId-123": { 
        required: true 
    }
}

通常,如果您将这些名称用作JavaScript目标,则应避免使用破折号-命名,因为JavaScript有时会将其解释为减号.对于jQuery Validate,引号将解决此问题.

http://jsfiddle.net/RB4NU/12/

I have a online survey and I am using jquery validation on it.

So each question has 4 options, where each option is a radio button.

When I am building my jquery validatio rules, how should I reference them?

Each question option has an ID and name.

Below is what I am doing so far, and it isn't working.

$(document).ready(function() {

 $("#myForm").validate({
           rules : {     
               someQuestionId123 : {required : true }
           });
  });

Where someQuestionId123 is the name of the radio button element.

解决方案

You're improperly closing your rules with a });...

rules : {     
    someQuestionId123 : {required : true }
});


Using the jQuery Validate plugin's .validate() method, you must reference them by name.

Since a group of radio buttons should all share the same name, you're already doing it properly (after fixing an issue with your brackets)...

$(document).ready(function() {

    $("#myForm").validate({
        rules: {     
            someQuestionId123: { // <-- NAME attribute
                required: true 
            }
        }  // <-- right here, you incorrectly had '});'
    });
});

HTML:

<input type="radio" name="someQuestionId123" value="one" />
<input type="radio" name="someQuestionId123" value="two" />
<input type="radio" name="someQuestionId123" value="three" />
<input type="radio" name="someQuestionId123" value="four" />

DEMO: http://jsfiddle.net/5dsJQ/


And finally, your latest jsFiddle is broken because you have special characters, such as -, inside your name...

rules: {     
    someQuestionId-123: { 
        required: true 
    }
}

As per the "reference docs", you need to enclose the name in quotes to fix this issue...

rules: {     
    "someQuestionId-123": { 
        required: true 
    }
}

In general, if you're using these names as JavaScript targets, you'd avoid naming with dashes, -, because JavaScript could sometimes interpret them as minus signs. For jQuery Validate, the quotation marks will get around this problem.

http://jsfiddle.net/RB4NU/12/

这篇关于如何验证一组单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆