jQuery选择框验证 [英] jQuery select box validation
问题描述
我在jQuery中有以下代码.
当select
元素的选项值为"0"时,我需要显示一条验证消息"Year Required".
<script type="text/javascript">
$(document).ready(function () {
$("#register").validate({
debug: true,
rules: {
year: {
required: function () {
if ($("#year option[value='0']")) {
return true;
} else {
return false;
}
}
}
},
messages: {
year: "Year Required",
},
});
})
</script>
选择框
<select name="year" id="year">
<option value="0">Year</option>
<option value="1">1919</option>
<option value="2">1920</option>
<option value="3">1921</option>
<option value="4">1922</option>
</select>
由于无法在第一个 HTML : 工作示例: http://jsfiddle.net/tPRNd/ 原始答案 :(仅当您可以在第一个 要使用jQuery Validate插件正确地验证 jQuery : HTML : 演示: http://jsfiddle.net/XGtEr/ > I have following code in jQuery. Select box
Since you cannot set jQuery: HTML: Working Demo: http://jsfiddle.net/tPRNd/ Original Answer: (Only if you can set To properly validate a jQuery: HTML: Demo: http://jsfiddle.net/XGtEr/ 这篇关于jQuery选择框验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!option
中设置value=""
,因此您需要使用<select name="year">
<option value="0">Year</option>
<option value="1">1955</option>
<option value="2">1956</option>
</select>
option
中设置value=""
时)select
元素,只需要第一个option
包含value=""
.因此,将0
从value="0"
删除,它已修复.$(document).ready(function () {
$('#myform').validate({ // initialize the plugin
rules: {
year: {
required: true,
}
}
});
});
<select name="year">
<option value="">Year</option>
<option value="1">1955</option>
<option value="2">1956</option>
</select>
I need to display a validation message "Year Required" when the option value of select
element is "0". <script type="text/javascript">
$(document).ready(function () {
$("#register").validate({
debug: true,
rules: {
year: {
required: function () {
if ($("#year option[value='0']")) {
return true;
} else {
return false;
}
}
}
},
messages: {
year: "Year Required",
},
});
})
</script>
<select name="year" id="year">
<option value="0">Year</option>
<option value="1">1919</option>
<option value="2">1920</option>
<option value="3">1921</option>
<option value="4">1922</option>
</select>
value=""
within your first option
, you'll need to create your own rule using the built-in addMethod()
method.$(document).ready(function () {
$('#myform').validate({ // initialize the plugin
rules: {
year: {
selectcheck: true
}
}
});
jQuery.validator.addMethod('selectcheck', function (value) {
return (value != '0');
}, "year required");
});
<select name="year">
<option value="0">Year</option>
<option value="1">1955</option>
<option value="2">1956</option>
</select>
value=""
within the first option
)select
element with the jQuery Validate plugin simply requires that the first option
contains value=""
. So remove the 0
from value="0"
and it's fixed.$(document).ready(function () {
$('#myform').validate({ // initialize the plugin
rules: {
year: {
required: true,
}
}
});
});
<select name="year">
<option value="">Year</option>
<option value="1">1955</option>
<option value="2">1956</option>
</select>