jQuery验证-仅当填充另一个字段时才需要字段 [英] jQuery Validation - require field only if another field is filled
本文介绍了jQuery验证-仅当填充另一个字段时才需要字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
JQuery验证-如果复选框已选中则为必需
Possible Duplicate:
JQuery validation-select required if checkbox checked
我正在使用jQuery Validation验证表单.我正在尝试提出一个规则,该规则将在填充一组特定字段中的某个字段时根据需要验证字段:
I'm validating a form with jQuery Validation. I'm trying to come up with a rule that will validate fields as required if a field in a particular set of fields is filled:
<form id="donate-form">
<input type="text" name="full_name" required="required" /><br />
<input type="text" name="address1" /><br />
<input type="text" name="address2" /><br />
<input type="text" name="city" /><br />
<select name="state">
<option value="">- State -</option>
<option value="va">Virginia</option>
<option value="md">Maryland</option>
</select><br />
<input type="text" name="zip" />
<input type="submit" name="submit" />
</form>
所有地址字段都是可选的,除非有人填写地址1,城市,州,或邮政编码,否则都必须填写.
All address fields are optional unless someone fills out address1, city, state, or zip, then they should all be required.
推荐答案
使用depends
,填写您的要求.
var depends = function() {
var vals = '';
$('form > *').not(':eq(0)').each(function () {
vals += $(this).val();
});
return vals.length > 0;
};
您应该将选择器更改为更具体的选项,以满足您的需求.即不是$('form')
You should change the selectors to something more specific to fit your needs. ie not $('form')
这篇关于jQuery验证-仅当填充另一个字段时才需要字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文