如何将 jquery.Validate 与 jquery.multiselect 下拉菜单一起使用?
所以情况是这样的:尝试使用 jquery.multiselect< 添加下拉框当前使用 jquery.validate 插件的表单上的 /a> 插件有其他字段(文本输入字段、具有浮点值范围的单个文本输入)当前均已正确验证。
当我尝试添加验证规则时,我根本无法让 jquery.validate 来验证我的多选下拉列表。以下是我的代码片段(全部假设已加载所需插件 - 请参阅下文了解使用的版本):
HTML:
<form action="some/action" id="myForm" method="POST">
Input 1: <input type="text" value="" name="input1" maxlength="200" id="input1"><br/>
Input 2: <input type="text" value="" name="input2" maxlength="100" id="input2"><br/>
Input 3: <input type="text" value="" name="input3" maxlength="50" id="input3"><br/>
Select: <select class="someSelect" name="mySelect" id="mySelect" multiple="multiple">
<option value="some_val1">Some Value</option>
<option value="some_val2">Some Other Value</option>
</select>
<input type="submit" value="Submit" />
</form>
Javascript:
$(document).ready(function() {
$('#mySelect').multiselect({
noneSelectedText: 'Select Something (required)',
selectedList: 3,
classes: 'my-select'
});
$.validator.addMethod("needsSelection", function(value, element) {
return $(element).multiselect("getChecked").length > 0;
});
$.validator.addMethod("isPercent", function(value, element) {
return parseFloat(value) >= 0 && parseFloat(value) <= 100;
});
$.validator.messages.needsSelection = 'You gotta pick something.';
$.validator.messages.isPercent = 'Must be between 0% and 100%';
$('#myForm').validate({
rules: {
mySelect: "required needsSelection",
input1: "required isPercent",
input2: "required",
input3: "required"
},
errorClass: 'invalid'
});
});
版本 如果版本之间存在明显/已知的兼容性问题,那么如果可以解决问题,我可能会进行升级,但我已经出于我的目的使用最新版本进行了测试,但它似乎没有解决我的问题。
jQuery:1.4.4
jquery.validate:1.9.0
jquery.multiselect:1.8
并且,一如既往,我可以尽可能提供更多信息/需要。
So the situation is this: attempting to add a dropdown box using the jquery.multiselect plugin on a form that current uses the jquery.validate plugin that has other fields (text input fields, single text input that has a float
value range) that all currently validate correctly.
When I attempt to add validation rules I simply cannot get jquery.validate to validate my multiselect dropdown whatsoever. Here are snippets of my code (all assumes that required plugins are loaded - see below for versions used):
The HTML:
<form action="some/action" id="myForm" method="POST">
Input 1: <input type="text" value="" name="input1" maxlength="200" id="input1"><br/>
Input 2: <input type="text" value="" name="input2" maxlength="100" id="input2"><br/>
Input 3: <input type="text" value="" name="input3" maxlength="50" id="input3"><br/>
Select: <select class="someSelect" name="mySelect" id="mySelect" multiple="multiple">
<option value="some_val1">Some Value</option>
<option value="some_val2">Some Other Value</option>
</select>
<input type="submit" value="Submit" />
</form>
The Javascript:
$(document).ready(function() {
$('#mySelect').multiselect({
noneSelectedText: 'Select Something (required)',
selectedList: 3,
classes: 'my-select'
});
$.validator.addMethod("needsSelection", function(value, element) {
return $(element).multiselect("getChecked").length > 0;
});
$.validator.addMethod("isPercent", function(value, element) {
return parseFloat(value) >= 0 && parseFloat(value) <= 100;
});
$.validator.messages.needsSelection = 'You gotta pick something.';
$.validator.messages.isPercent = 'Must be between 0% and 100%';
$('#myForm').validate({
rules: {
mySelect: "required needsSelection",
input1: "required isPercent",
input2: "required",
input3: "required"
},
errorClass: 'invalid'
});
});
Versions
If there's an explicit/known issue with compatibility between the versions, then I may upgrade if that solves the issue, but I've tested using the newest versions for my purposes and it did not seem to solve my issue.
jQuery: 1.4.4
jquery.validate: 1.9.0
jquery.multiselect: 1.8
And, as always, I can provide more information where possible/needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,看来我为多选设置的规则确实附加到了选择,但是,由于原始选择框是
:hidden
byjquery.multiselect
,jquery.validate
默认情况下会忽略任何隐藏的输入。解决方案(不是我自己的 - 同事发现的)是使用
jquery.validate
的ignore
设置。无论使用ignore
设置传入什么选择器,都会将其放入 jquery.not()
中,因此解决方案实际上是在这里使用双重否定:唷!
So it seems that the rules I was setting up for the multiselect were indeed being attached to the select, however, since the original select box is
:hidden
byjquery.multiselect
,jquery.validate
by default ignores any hidden inputs.The solution (which is not of my own - a coworker found it) is to use the
ignore
settings forjquery.validate
. Whatever selector is passed in with theignore
setting is put into a jquery.not()
, so the solution is actually to use a double negative here:Phew!
在我看来,您的两个选择选项具有价值。
尝试在顶部添加一个值为空的选项并选择:
Looks to me like your two select options carry a value.
Try adding an option at the top with a value of nothing and selected:
另一种可能的解决方案是显式指定要验证的元素:
这也将验证隐藏的选择。
事实上,我刚刚遇到的情况没有其他方法,因为我想对多步骤表单进行部分验证:
希望它对某人有帮助
Another possible solution is to explicitly specify the elements to validate:
That will validate the hidden select as well.
In fact, the situation I just encountered there was no other way since I wanted to validate by part for a multistep form:
Hope it helps someone