jQuery 选择框验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14896854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery select box validation
提问by Tom
I have following code in jQuery.
I need to display a validation message "Year Required" when the option value of select
element is "0".
我在 jQuery 中有以下代码。
当select
元素的选项值为“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 box
选择框
<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>
回答by Sparky
Since you cannot set value=""
within your first option
, you'll need to create your own rule using the built-in addMethod()
method.
由于您无法value=""
在您的 first 中设置option
,您需要使用内置addMethod()
方法创建您自己的规则。
jQuery:
jQuery:
$(document).ready(function () {
$('#myform').validate({ // initialize the plugin
rules: {
year: {
selectcheck: true
}
}
});
jQuery.validator.addMethod('selectcheck', function (value) {
return (value != '0');
}, "year required");
});
HTML:
HTML:
<select name="year">
<option value="0">Year</option>
<option value="1">1955</option>
<option value="2">1956</option>
</select>
Working Demo: http://jsfiddle.net/tPRNd/
工作演示:http: //jsfiddle.net/tPRNd/
Original Answer:(Only if you can set value=""
within the first option
)
原始答案:(仅当您可以value=""
在第一个内设置时option
)
To properly validate a 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.
要使用select
jQuery Validate 插件正确验证元素,只需要第一个option
包含value=""
. 所以删除0
fromvalue="0"
并修复它。
jQuery:
jQuery:
$(document).ready(function () {
$('#myform').validate({ // initialize the plugin
rules: {
year: {
required: true,
}
}
});
});
HTML:
HTML:
<select name="year">
<option value="">Year</option>
<option value="1">1955</option>
<option value="2">1956</option>
</select>
回答by Dr Blowhard
To put a require rule on a select list you just need an option with an empty value
要将 require 规则放在选择列表上,您只需要一个带有空值的选项
<option value="">Year</option>
then just applying required on its own is enough
那么只需要自己申请就足够了
<script>
$(function () {
$("form").validate();
});
</script>
with form
有形式
<form>
<select name="year" id="year" class="required">
<option value="">Year</option>
<option value="1">1919</option>
<option value="2">1920</option>
<option value="3">1921</option>
<option value="4">1922</option>
</select>
<input type="submit" />
</form>
回答by Anish Mathew
http://docs.jquery.com/Plugins/Validation/Methods/required
http://docs.jquery.com/Plugins/Validation/Methods/required
edit the code for 'select' as below for checking for a 0 or null value selection from select list
编辑“选择”的代码如下,以检查选择列表中的 0 或空值选择
case 'select':
var options = $("option:selected", element);
return (options[0].value != 0 && options.length > 0 && options[0].value != '') && (element.type == "select-multiple" || ($.browser.msie && !(options[0].attributes['value'].specified) ? options[0].text : options[0].value).length > 0);
回答by Anil chhabra
Jquery Select Box Validation.You can Alert Message via alert or Put message in Div as per your requirements.
Jquery 选择框验证。您可以根据您的要求通过警报或将消息放入 Div 来发出警报消息。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="message"></div>
<form method="post">
<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>
<button id="clickme">Click</button>
</form>
<script>
$("#clickme").click(function(){
if( $("#year option:selected").val()=='0'){
alert("Please select one option at least");
$("#message").html("Select At least one option");
}
});
</script>
回答by Mark Schultheiss
simplify the whole thing a bit, fix some issues with commas which will blow up some browsers:
稍微简化整个事情,用逗号修复一些问题,这会炸毁一些浏览器:
$(document).ready(function () {
$("#register").validate({
debug: true,
rules: {
year: {
required: function () {
return ($("#year option:selected").val() == "0");
}
}
},
messages: {
year: "Year Required"
}
});
});
Jumping to an assumption, should your select not have this attribute validate="required:true"
?
跳到一个假设,你的选择应该没有这个属性validate="required:true"
吗?