jQuery validate only if a specific radio button is selected

匿名 (未验证) 提交于 2019-12-03 02:51:02

问题:

I got stuck at a particular situation

I have 2 radio buttons

<input type="radio" value="YES" id="sub" name="sub">Yes <input type="radio" value="NO" id="sub" name="sub">No 

When, Yes is selected, I need to validated 1 text-field

<input type="text" id="price" name="price" /> 

I am using jQuery validation plug-in

Please help me with this, Thanks.

回答1:

You can set up dependency validation as seen here http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-expression

$('#myForm').validate(rules: {    price: {       required: '#sub[value="YES"]:checked'     } }); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!