2

仅当“select1”字段中的选定文本为“其他”时,我才想将“其他”字段设为必需,我正在尝试的规则是:other: { required: function(element){ return $("#select1 option:selected").text() == "Other"; } }我做错了什么吗?这是代码:

<!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>
4

4 回答 4

1

更新了答案在这里找到一个小提琴演示:http: //jsfiddle.net/3jrTM/

$('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.");​
于 2012-12-18T18:31:21.127 回答
1

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

  1. 这里额外的一个closing bracket

    select1: { valueNotEquals: "0" }
        },
    
  2. 在定义结尾和定义开始之前closing bracket缺少一个rulesmessages

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

  3. comma每个选项末尾的额外内容是不必要的

  4. validate.js 路径中的空格(这可能是问题 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"}
            }
});
...
于 2012-12-18T18:44:38.777 回答
0
$('select').change(function() {
   var text = $('select option:selected').text();​​​​​​​​​
   if(text == 'Other') {
       $('input[name=other]').attr('disabled', false);        
    }
  });

这就是你的意思吗

于 2012-12-18T18:19:15.017 回答
0

您可以删除包含“0”的选项值。

<option>Select here someting</option>

当一个选项不包含值atroibute时,jQuery验证将其致电或未给出。

<select class="required"><option>Select here someting</option></select>

您可以将“必需”作为类添加到您的选择框,以便 jQuery 验证将提示“必需”警告。

于 2012-12-18T18:29:49.933 回答