简体   繁体   English

jQuery验证插件以确保Materialize CSS需要Select选项吗?

[英]JQuery validate plugin to ensure a Select Option is required with Materialize CSS?

  $(document).ready(function() { $('select').material_select(); }); $.validator.setDefaults({ errorClass : 'invalid', validClass : "valid", errorPlacement : function(error, element) { $(element).closest("form").find( "label[for='" + element.attr("id") + "']").attr('data-error', error.text()); }, submitHandler : function(form) { console.log('form ok'); } }); $.validator.addMethod("valueNotEquals", function(value, element, arg) { return arg != value; }, "Value must not equal arg."); $("#form").validate({ rules : { dateField : { date : true }, world_region : { valueNotEquals : "default" } }, messages : { world_region : { valueNotEquals : "This field is required. Please select an item." } } }); 

The following JSFiddle has the code I am trying to implement. 以下JSFiddle具有我要实现的代码。 https://jsfiddle.net/869mdk5a/6/ https://jsfiddle.net/869mdk5a/6/

What I am trying to do is require a selection on a select/option dropdown. 我想做的是需要在选择/选项下拉列表中进行选择。 If you click the submit button in the fiddle you will see that all the text field inputs update to red and show "This field is required" under the input area. 如果单击小提琴中的“提交”按钮,您将看到所有文本字段输入都更新为红色,并在输入区域下方显示“此字段是必需的”。 How do I get the "This field is required. Please select an option" to show below where the select field is located? 如何获得“此字段为必填项,请选择一个选项”显示在选择字段的下方?

I wrote a validator addMethod and would expect it to work just like an input. 我写了一个验证器addMethod,希望它像输入一样工作。 Ideas? 想法?

Thanks! 谢谢!

All you need is add required:true to your code eg: 您所需要做的就是在代码中添加required:true,例如:

 world_region: {
      required:true,
      valueNotEquals: "default"
    }

hope this help you ! 希望这对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM