jQuery的问题在选择列表上验证 [英] Problems with jQuery Validate on Select List
问题描述
我已经看到大量关于如何在选择列表中使用jQuery Validation的示例 - 我正在尝试实现一个,但它只是对我不起作用。我对插件一般都是新手,我对我做错了完全感到困惑。
I've seen tons of samples of how to use jQuery Validation on a select list - and I'm attempting to implement one, but it just isn't functioning for me. I'm really new to the plugin in general and am just completely baffled about what I am doing wrong.
预期的行为是它不应该验证用户是否有将选择菜单保留为默认。他们必须做出选择。
The intended behavior is that it should not validate if the user has left the select menu on "default". They have to make a selection.
我首先连接新的验证器方法。
I start by wiring up the new validator method.
(function($) {
$.validator.addMethod('mustbe', function(value, element, params) {
var testValue = params['propertyvalue'];
var condition = params['condition'];
if ((condition == '0') && (value != testValue)) return true;
if ((condition == '1') && (value == testValue)) return true;
return false;
});
})(jQuery);
$(document).ready(function() {
$("#form1").validate();
$('#form1').submit(function() {
alert($('#form1').valid());
});
});
然后我通过将元数据添加到实际的HTML来进行跟进。
Then I followup by adding the metadata to the actual HTML.
<form id="form1" action="">
<select id="select_list" data-val="true"
data-val-mustbe="You must select a value"
data-val-mustbe-condition="0"
data-val-mustbe-propertyvalue="default"
data-val-required="You must select a value" >
<option value="default">Choose...</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="submit" id="submit" value="Submit" />
</form>
它只是拒绝验证。这里也是一个小提琴的链接:
And it just refuses to validate. Here is a link to a fiddle, too :
推荐答案
您应该已经告诉过您正在使用ASP.NET MVC。它没有用,但人们会停止询问自定义属性名称。
You should have told you are using ASP.NET MVC. It wouldn't help but people would stop asking about custom attribute names.
我重新设计了你的代码,以使用值default和data-val属性。这是一个代码示例:
I have redesigned your code to work with value default and your data-val attributes. This is a code sample:
$(document).ready(function() {
$.validator.addMethod("valueNotEquals", function(value, element, arg){
return arg != value;
}, "");
$("#form1").validate({
rules: {
select_list : {valueNotEquals: $('#select_list').attr('data-val-mustbe-propertyvalue')},
},
messages: {
select_list : { valueNotEquals: $('#select_list').attr('data-val-required') }
},
submitHandler: function(form) {
alert($('#form1').valid());
form.submit();
}
});
});
编辑:
这是一个jsFiddle示例: http://jsfiddle.net/Gajotres/ekPpS/
And here's a jsFiddle example: http://jsfiddle.net/Gajotres/ekPpS/
这是一个没有ASP.NET MVC属性的版本:
And here's a version without ASP.NET MVC attributes:
<form id="form1" action="">
<select id="select_list" name="select_list">
<option value="default">Choose...</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="submit" id="submit" value="Submit" />
</form>
$(document).ready(function() {
$.validator.addMethod("valueNotEquals", function(value, element, arg){
return arg != value;
}, "");
$("#form1").validate({
rules: {
select_list : {valueNotEquals: "default"},
},
messages: {
select_list : { valueNotEquals: "You must select a value" }
},
submitHandler: function(form) {
alert($('#form1').valid());
form.submit();
}
});
});
这篇关于jQuery的问题在选择列表上验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!