问题
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.
Javascript
(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());
});
});
Then I followup by adding the metadata to the actual HTML.
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 :
jsFiddle
回答1:
You should have told you are using ASP.NET MVC. It wouldn't help but people would stop asking about custom attribute names.
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();
}
});
});
EDIT :
And here's a jsFiddle example: http://jsfiddle.net/Gajotres/ekPpS/
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();
}
});
});
回答2:
The simple way is to use value='' in the default option, and specify that the select is required:
<select id="select_list" class="required">
回答3:
So the answer by Barmar is partially correct but not well explained.
changes I made to get this updated fiddle to work:
added name="select_list" to the select element itself to give jquery.validate what it wants to look for
modified the javascript validate call:
$("#form1").validate({
rules: {
select_list: {
required: true,
number: true
}
},
submitHandler: function(form) {
alert($('#form1').valid());
form.submit();
}
});
来源:https://stackoverflow.com/questions/14078482/problems-with-jquery-validate-on-select-list