简体   繁体   English

如何在SumoSelect下拉菜单中添加jQuery验证?

[英]How to add jQuery validation to SumoSelect drop downs?

I am looking for a way to validate multiple sumo select boxes. 我正在寻找一种验证多个相扑选择框的方法。 The code I have is as follows. 我的代码如下。

HTML 的HTML

<select class="kid-age form-select" id="edit-age-big-kid-1" name="age_big_kid_1">     
    <option value="">-</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
</select>

<select class="kid-age form-select" id="edit-age-big-kid-2" name="age_big_kid_2">     
    <option value="">-</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
</select>

<select class="kid-age form-select" id="edit-age-small-kid-1" name="age_small_kid_1">     
    <option value="">-</option>
    <option value="3">0</option>
    <option value="4">1</option>
    <option value="5">2</option>
</select>

JS JS

$('.kid-age').SumoSelect();

How can I client side validate all of the drop downs (there may be multiple) to be required and display a single message "All kid ages are required." 我如何在客户端验证所需的所有下拉列表(可能有多个),并显示一条消息“需要所有年龄段的孩子”。

Thanks in advance! 提前致谢!

Here is quick and simple validation, but it will give you idea, you can modify it to fit your needs: 这是快速简单的验证,但是它将为您提供想法,您可以对其进行修改以适合您的需求:

$(document).ready(function () {

   $('.kid-age').SumoSelect();
   $('#submit').on('click', function() {
       errors=[];
       $('p.SelectBox').each(function() {

          if($(this).attr('title')==" -") {



              errors.push('invalid');

          }

       });

        if(errors.length>0) { // if there are errors, show message, stop submission, etc...
               message='All kid ages are required.';
               $('.error').text(message);
          }
          else { //if no errors, continue with script execution...
              $('.error').text('fields validated');
          }
   });


});

Plugin creates one specific p tag (for all dropdowns) which holds selected options... so, you can check its value, and if different than ' -', continue script execution, otherwise error will be thrown. 插件会创建一个特定的p标签(用于所有下拉菜单),其中包含选定的选项...因此,您可以检查其值,如果与“-”不同,则继续执行脚本,否则将引发错误。

See it in action: https://jsfiddle.net/g7bLbevy/ 实际观看: https : //jsfiddle.net/g7bLbevy/

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

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