根据从下拉菜单中选择的选项添加验证规则 [英] Adding validation rule(s) depending on option selected from dropdown menu

查看:97
本文介绍了根据从下拉菜单中选择的选项添加验证规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次使用jquery validate插件.我处于一个有三个选项的选择框的情况.我需要做两件事:

I am using the jquery validate plugin for the first time. I am in a situation where I have a select box that has three options. I need to do two things:

  1. 使选择框为必填项.
  2. 根据选择的选项,使其他文本框为必填项.

所以...

<select id="selectBox">
    <option value selected></option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option Value="3">3</option>
</select>

Coord 1: <input type="text" name="coord1"><br>
Coord 2: <input type="text" name="coord2"><br>
Coord 3: <input type="text" name="coord3"><br>

  • 如果在下拉菜单中选择了选项1,则必须输入Coord 1.
  • 如果选择选项2,则需要坐标1和2.
  • 如果选择了选项3,则需要坐标1和坐标3.
  • 这如何向验证器添加自定义规则?

    How does this work adding custom rules to the validator?

    预先感谢...

    推荐答案

    尝试

     If you need starting itself 
    
        // $('input[type=text]:eq(0)').prop('required', true);
    
     $("#selectBox").change(function () {
        var index=this.value-1;
        $('input[type=text]:gt(0)').prop('required', false);
       //  $('input[type=text]:eq(0)').prop('required', true);
        $('input[type=text]:eq('+index+')').prop('required', true);
     });
    

    这篇关于根据从下拉菜单中选择的选项添加验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆