jQuery 验证必需选择
我正在尝试使用 jQuery Validate 插件验证 html select 元素。我将“必需”规则设置为 true,但它总是通过验证,因为默认选择零索引。有什么方法可以定义所需规则使用的空值吗?
UPD。例子。想象一下我们有以下 html 控件:
<select>
<option value="default">Choose...</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
我希望验证插件使用“默认”值作为空。
I am trying to validate html select element using jQuery Validate plugin. I set "required" rule to true but it always passes validation because zero index is chosed by default. Is there any way to define empty value that is used by required rule?
UPD. Example. Imagine we have the following html control:
<select>
<option value="default">Choose...</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
I want Validation plugin to use "default" value as empty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这里概述了一个更简单的解决方案:
验证选择框
将值设为空并添加必需的属性
An easier solution has been outlined here:
Validate select box
Make the value be empty and add the required attribute
您可以编写自己的规则!
You can write your own rule!
只需将第一个选项的值设置为空字符串
value=""
和 jquery 验证< /a>
必需
规则将起作用
just set the value of the first option to empty string
value=""
and jquery validation
required
rulewill work
使用最小规则
将第一个选项值设置为 0
use min rule
set first option value to 0
您只需将
validate[required]
作为此选择的类,然后放置一个带有 value="" 的选项,例如:
You only need to put
validate[required]
as class of this select and then put a option with value=""for example:
我不知道问题提出时(2010 年)这个插件怎么样,但我今天遇到了同样的问题并以这种方式解决了它:
给你的选择标签一个名称属性。例如本例
不要使用标签选项中的属性 value="default",而是禁用默认选项或设置 value=" “按照安德鲁·科茨的建议
<选项禁用=“禁用”>选择...
或
设置插件验证规则
$( "#YOUR_FORM_ID" ).validate({
规则:{
myselect: { 必需: true }
}
});
或
观察:Andrew Coats 的解决方案仅在表单中只有一项选择时才有效。如果您希望他的解决方案适用于多个选择,请向您的选择添加名称属性。
希望有帮助! :)
I don't know how was the plugin the time the question was asked (2010), but I faced the same problem today and solved it this way:
Give your select tag a name attribute. For example in this case
<select name="myselect">
Instead of working with the attribute value="default" in the tag option, disable the default option or set value="" as suggested by Andrew Coats
<option disabled="disabled">Choose...</option>
or
<option value="">Choose...</option>
Set the plugin validation rule
$( "#YOUR_FORM_ID" ).validate({
rules: {
myselect: { required: true }
}
});
or
<select name="myselect" class="required">
Obs: Andrew Coats' solution works only if you have just one select in your form. If you want his solution to work with more than one select add a name attribute to your select.
Hope it helps! :)
JQuery:
JQuery :
选择值将为空
select value will be blank
@JMP 提到的解决方案适用于我的情况,只需稍加修改即可:
我在
addmethod
中使用element.value
而不是value
。可能是我这里有特殊情况,但没有找出原因。但@JMP 的解决方案应该适用于常规情况。
The solution mentioned by @JMP worked in my case with a little modification:
I use
element.value
instead ofvalue
in theaddmethod
.It could be possible, that I have a special case here, but didn't track down the cause. But @JMP's solution should work in regular cases.
很简单,您需要获取 Select 字段值,并且它不能是“默认”。
It's simple, you need to get the Select field value and it cannot be "default".
如何验证选择“qualifica”它有 3 个选择
how to validate the select "qualifica" it has 3 choose