jQuery Validate:不是默认值

编程入门 行业动态 更新时间:2024-10-25 06:20:06
本文介绍了jQuery Validate:不是默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的表单中有一个类别下拉选择元素.表单已通过jQuery验证,如下所示:

I have a category dropdown selection element in my form. The form is validated with jQuery as follows:

$('#myForm').validate({ rules: { text: { required: true }, category: { required: true } }, messages: { text: { required: "Text required" }, category: { required: "Category required" } }

加载页面时,类别的默认值为:'---'(不带引号).当我提交表格时,验证说一切都可以,因为"---"不是什么.如果将"---"用作类别,如何停止提交?其他类别如下:Category1.必须选择一种可能性,而我无法更改下拉选择项.

When loading the page, the default value of category is: '---' (without quotes). When I submit the form, the validation says eveything is ok, because '---' is not nothing. How can I get it to stop submitting when '---' is used as category? The other categories are something like this: Category1. One of the possibilities have to be choosen and I can't change the dropdown selection thing.

谢谢!

推荐答案

您可以添加自定义规则,如下所示:

You could add a custom rule, like this:

jQuery.validator.addMethod("notEqual", function(value, element, param) { return this.optional(element) || value !== param; }, "Please choose a value!");

然后在您的rules中使用该规则,如下所示:

Then use that rule in your rules, like this:

category: { required: true, notEqual: "---" }

您可以在此处进行测试.

更多推荐

jQuery Validate:不是默认值

本文发布于:2023-11-17 03:19:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608627.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:默认值   jQuery   Validate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!