JQuery 添加验证方法来验证表单中的另一个元素
我正在尝试在 JQuery 中创建自定义验证方法,但无法使其正常工作。
这就是我想要做的:
<div class="row">
<label for="fld-stories">Stories</label>
<select id="fld-stories" class="valid" name="stories" title="">
<option value="">--</option>
<option value="1" label="1">1</option>
<option value="2" label="2">2</option>
<option value="3" label="3">3</option>
<option value="4" label="4">4</option>
<option value="5" label="5">5</option>
</select>
</div>
<div id="row_master_bedroom_location" class="row">
<label for="fld-master_bedroom_location">Master Bedroom Location</label>
<select id="fld-master_bedroom_location" name="master_bedroom_location" title="">
<option value="up">Upstairs</option>
<option value="down">Downstairs</option>
</select>
</div>
如果用户选择主卧室位置在楼上(参见第二个选择),则房屋的层数应大于或等于 2(这是合乎逻辑的,因为主卧室不能如果只有一层,就在楼上)。
为此,我尝试向 JQuery 验证器添加自定义方法:
$.validator.addMethod('masterBedroomLocation', function(value, element, params) {
if (value == 'up' && $("#fld-stories").val() < 2){
return false;
}
}, 'You cannot select "Upstairs" for a one–story home');
我不确定如何获取表单中另一个元素的值,我尝试了 JQuery 旧时尚方式,但它似乎不起作用。
有关更多信息,我在全局 javascript 文件中添加了自定义方法,该函数将 JQuery 验证绑定到具有特殊类名的每个表单。
I'm trying to create a custom validation method in JQuery and I can't manage to make it work properly.
Here is what I want to do :
<div class="row">
<label for="fld-stories">Stories</label>
<select id="fld-stories" class="valid" name="stories" title="">
<option value="">--</option>
<option value="1" label="1">1</option>
<option value="2" label="2">2</option>
<option value="3" label="3">3</option>
<option value="4" label="4">4</option>
<option value="5" label="5">5</option>
</select>
</div>
<div id="row_master_bedroom_location" class="row">
<label for="fld-master_bedroom_location">Master Bedroom Location</label>
<select id="fld-master_bedroom_location" name="master_bedroom_location" title="">
<option value="up">Upstairs</option>
<option value="down">Downstairs</option>
</select>
</div>
If the user selects the master bedroom location to be upstairs (see second select), the number of stories for the house should be superior or equal to 2 (which is logical since the master bedroom can't be upstairs if there is only one story).
To do so I try to add a custom method to my JQuery validator :
$.validator.addMethod('masterBedroomLocation', function(value, element, params) {
if (value == 'up' && $("#fld-stories").val() < 2){
return false;
}
}, 'You cannot select "Upstairs" for a one–story home');
I'm not sure how to get the value of another element in the form, I tried the JQuery old fashion way but it doesn't seem to work.
For additional information, I add the custom method in a global javascript files, in a function that binds the JQuery validation to every forms that have a special class name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我设法让它发挥作用。
看起来像 Jquery 选择器
$(“#fld-故事”)
工作正常。
由于某种我不知道的原因,如果我反转检查方式并返回 true 而不是 false,则该方法会起作用。
这是新方法:
I managed to make it work.
It looks like the Jquery selector
$("#fld-stories")
works properly.
For some reason I don't know, the method works if I invert the way I do my check and return a true instead of a false.
Here is the new method :
我稍微改进了我的代码。
这是新方法:
我希望这可以帮助别人。
I improved my code a little bit.
Here is the new method :
I hope this can help somebody.