使用jquery进行条件验证

时间:2011-02-10 09:43:41

标签: jquery validation conditional

我有一个带有多个单选按钮选项的表单,都是必需的。

但是我想在一个选择的单选按钮上添加额外的验证。

例如:

如果选择的值等于“Brown”,则仅需要对此颜色进行进一步验证,例如

input:radio[name='radTypeJoint']:checked").val() == 'Shear' 
&& 
input:radio[name='radSubstrate']:checked").val() == 'Masonry to Masonry'

return false;

任何想法,非常感谢Adam

我现在已经明白了:

radShade: { required: function(element) {  
                if (($("input:radio[name='radShade']:checked").val() == 'Brown') && ($("input:radio[name='radGeographical']:checked").val() == 'South Facing') || ($("input:radio[name='radShade']:checked").val() == 'Brown') && ($("input:radio[name='radGeographical']:checked").val() == 'West Facing') || ($("input:radio[name='radShade']:checked").val() == 'Brown') && ($("input:radio[name='radMovement']:checked").val() != 'Increase of 12 of minimum joint width') || ($("input:radio[name='radShade']:checked").val() == 'Brown') && ($("input:radio[name='radStories']:checked").val() != 'Less than 3') || ($("input:radio[name='radShade']:checked").val() == 'Brown') && ($("input:radio[name='radSubstrate']:checked").val() != 'Masonry to Masonry') || ($("input:radio[name='radShade']:checked").val() == 'Brown') && ($("input:radio[name='radElements']:checked").val() == 'Window') || ($("input:radio[name='radShade']:checked").val() == 'Brown') && ($("input:radio[name='radTypeJoint']:checked").val() == 'Fillet')) {
                alert("This shade is not available with your current selections");
                return false;
                };
            }

但即使我返回true或返回false,表单仍然会被提交。

1 个答案:

答案 0 :(得分:1)

检查出来:

<强> HTML:

<input name="radiobutton" type="radio" value="brown" id="all" runat="server" class="cssRadio"/>
<input name="radiobutton" type="radio" value="black" id="Radio1" runat="server" class="cssRadio"/>
<input name="radiobutton" type="radio" value="red" id="Radio3" runat="server" class="cssRadio"/>

<强> JQUERY:

$(".cssRadio").click(function(){
    if($(this).val()=="brown")
    {
        //here u get the object of clicked radion button now do your validation
        alert($(this).val());
    }
    if($(this).val()=="black")
    {
        //here u get the object of clicked radion button now do your validation
        alert($(this).val());
    }
});