问题描述
我正在使用jQuery validate插件来验证我的表单.我想将几对字段绑定在一起,以便如果一个字段具有值,则另一个字段也必须具有一个值.本质上,两个字段(均为文本输入)必须都具有值或均不具有值.有人知道实现此目标的好方法吗?
I am using the jQuery validate plugin to validate my form. I would like to tie several pairs of fields together such that if one field has a value, the other is required to have a value also. Essentially both fields (both text inputs) must either both have a value or both not have a value. Is anyone aware of a good way to accomplish this?
推荐答案
如果您查看文档页面,您可以设置一个depends
字段.
if you look at the "rules" section's example code in the documentation page, there is a depends
field you can set.
类似于以下内容(这是我的头,未经测试):
something like the following (this is right off my head, not tested):
...
secondInput: {
required: function(element){
return $("#firstInput").val()!="";
}
}
....
这篇关于jQuery验证插件require字段(如果另一个字段具有值),反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!