JQuery 验证:必需(依赖表达式)
我希望仅当选择特定选项或选择特定单选按钮或在文本输入字段中输入特定字符串时才需要某个字段(我可以找到的所有必需(依赖表达式)示例都假设依赖性是在文本字段的情况下输入是否被选中/取消选中或填充/未填充)
这是我需要的示例,基于单选部分:
婚姻状况(必填字段):
<input name="status" id='status'type="radio" value="couple" checked="checked" />
<input name="status" id='status'type="radio" value="single male" />
<input name="status" id='status'type="radio" value="single female" />
合作伙伴姓名(依赖字段 -仅当选择“情侣”时才需要上面)
<input name="partner" type="text" id="partner">
示例 2:必填字段 = 地点
<select name="venue" id="venue">
<option value="">Please select venue</option>
<option>London EC1</option>
<option>London E9</option>
<option>Birmingham</option>
等。 依赖字段=用户(仅上述选择之一需要,例如“伯明翰”)
<input name="user" type="text" id="user">
如何调整http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-expression中显示的示例以适应上述内容情况?
$("#myform").validate({
rules: {
details: {
required: "#other:checked"
}
}, debug:true
});
$("#other").click(function() {
$("#details").valid();
});
我已经尝试过:
user: {
required: "#venue:('birmingham')"
}
但是
partner: {
required: "#status:('couple')"
}
这些都会产生使用户和合作伙伴需要的效果,无论场地和状态如何
I want a field to become required only when a specific option is selected or a specific radio button is selected or when a specific string is entered in a text input field (All of the required( dependency-expression ) examples I can find assume that the dependency is whether an input is checked / unchecked or filled/unfilled in the case of text fields)
Here is an example of what I need, based on a radio section:
Marital Status (the required field):
<input name="status" id='status'type="radio" value="couple" checked="checked" />
<input name="status" id='status'type="radio" value="single male" />
<input name="status" id='status'type="radio" value="single female" />
Partners Name (the dependent field - required only when 'Couple' is selected from above)
<input name="partner" type="text" id="partner">
Example 2: Required field = Venue
<select name="venue" id="venue">
<option value="">Please select venue</option>
<option>London EC1</option>
<option>London E9</option>
<option>Birmingham</option>
etc.
Dependent field = user (only required for one of the above selections, say 'Birmingham')
<input name="user" type="text" id="user">
How do I adapt the example shown athttp://docs.jquery.com/Plugins/Validation/Methods/required#dependency-expression for the above situations?
$("#myform").validate({
rules: {
details: {
required: "#other:checked"
}
}, debug:true
});
$("#other").click(function() {
$("#details").valid();
});
I have tried:
user: {
required: "#venue:('birmingham')"
}
and
partner: {
required: "#status:('couple')"
}
but these have the effect of making user and partner required regardless of results in venue and status
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于第一个,您的无线电输入应该有不同的 id:
然后您的规则可以如下所示:
对于第二个,只需使您的规则如下:
应该可以解决问题。
For the first one, your radio inputs should have different id's:
Then your rule can look like this:
For the second one, just make your rule like this:
Should do the trick.