Jquery基于选择文本的条件验证 [英] Jquery Conditional validation based on select text

查看:55
本文介绍了Jquery基于选择文本的条件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果select1字段中的所选文本是其他我想要的规则是:其他:{required:function(element) ){return $(#select1 option:selected)。text()==Other; } 我做错了什么?这是代码:

I want to make the "other" field required ONLY if the selected text in the "select1" field is "other" the rule I'm trying is: other: { required: function(element){ return $("#select1 option:selected").text() == "Other"; } } Am i doing something wrong? Here's the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript" src="http://jzaefferer.github.com/jquery-  validation/jquery.validate.js"></script>
  <script>
  $(document).ready(function(){
    $('form:first').validate({
        rules: {
          cname: {
          required: true,
        },
        select1: { valueNotEquals: "0" }
        },
        other: {
                required: function(element){
                            return $("#select1 option:selected").text() == "Other";
                        }
        },   
        messages: {
          cname: "Please enter a valid naaaammmeeee.",
      select1: "Please choose a valid option",
      other: "Please enter a valid other value",
        },
      });

       $.validator.addMethod("valueNotEquals", function(value, element, arg){
        return arg != value;
        }, "Value must not equal arg.");
  });
  </script>

</head>
<body>


 <form class="cmxform" id="commentForm" method="get" action="">
 <fieldset>
   <legend>A simple comment form with submit validation and default messages</legend>
   <p>
     <label for="cname">Name</label>
     <em>*</em><input id="cname" name="cname"/>
   </p>
   <p>
   <select name="select1" id="select1">
    <option value="0">Please choose a value</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">Other</option>
    </select>
    </p>
    <p>
    <label for="other">Other</label>
        <em>*</em><input id="other" name="other"/>
   </p>
       <p>
       <input class="submit" type="submit" value="Submit"/>
       </p>
 </fieldset>
 </form>
</body>
</html>


推荐答案

有多个问题主要与JSON格式有关:

There were multiple problems mostly related to JSON formatting:


  1. 一个额外的结束括号这里:

select1: { valueNotEquals: "0" }
    },


  • 规则结束括号 >定义,就在消息开始之前定义

    return $(#select1 option:selected)。 text()==其他;
    }
    }
    }

    留言:{

    return $("#select1 option:selected").text() == "Other"; } } },
    messages: {

    每个选项末尾的额外逗号是不必要的

    Extra comma at end of each options were un-necessary

    validate.js路径中的空格(这可能是问题中的一个拼写错误)

    Space in the validate.js path (this might be a typo here in the question itslef)

    这里是更正后的代码:

    ...

        <script src="http://code.jquery.com/jquery-latest.js"></script>
          <script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
          <script>
            $(document).ready(function(){
        $('form:first').validate({
        rules: {
            cname: { required: true },
            select1: { valueNotEquals: "0" },
            other: { required: function(element){
                                return $("#select1 option:selected").text() == "Other";
                                }
                }
    },   
            messages: {
                cname: "Please enter a valid naaaammmeeee.",
                select1: "Please choose a valid option",
                other: { required: "Please enter a valid other value"}
                }
    });
    ...
    

    这篇关于Jquery基于选择文本的条件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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