2012-10-03 174 views
1

使用从验证插件的这个例子中“需要(依赖表达式)”的解释:jQuery验证规则依赖

$("#myform").validate({ 
    rules: { 
    details: { 
     required: "#other:checked" 
    } 
    }, debug:true 
}); 
$("#other").click(function() { 
    $("#details").valid(); 
}); 

我试图做所需的文本输入,如果单选按钮#guide在选择下面的例子:

<input type="radio" id="outfitter" name="memtype" value="Outfitter" />Outfitter $125 
<input type="radio" id="guide" name="memtype" value="Guide" />Guide $75 
<input type="text" id="sponout" name="sponout" size="75" /> 

我只是不知道在哪里放置

$("#other").click(function() { 
    $("#details").valid(); 
}); 

规则验证鳕鱼内ING。

回答

2

这里是你可以做到这一点的一种方法:

$("#myform").validate({ 
    rules: { 
     sponout: { 
      required: "#guide:checked" 
     } 
    } 
}); 

$("input[name='memtype']").change(function() { 
    $("#myform").valid(); 
}); 

例子:http://jsfiddle.net/kqczf/1/

+0

OK,疑难杂症。我会给呃呃。 Thanx,特别是小提琴的例子。 – robincham

+0

丢弃了最终的'.change函数'代码部分,工作正常。实现我可以简单地添加必要条件:“条件术语”,正如你所指出的,安德鲁。 – robincham

+0

很高兴能帮到你!如果帮助':)'请接受答案' –