0

我目前正在处理这个; http://www.geektantra.com/2009/09/jquery-live-form-validation/

我正在尝试添加验证,因此如果用户选中“位置”框,则必须需要“L2”字段。我怎样才能用这个插件实现这一点?我是否必须混合复选框表达式?

expression: "if (isChecked(SelfID)) return true; else return false;",

这是插件功能

<script type="text/javascript">
            jQuery(function(){
                jQuery("#v1").validate({
                    expression: "if (VAL) return true; else return false;",
                    message: "Please enter your name"
                });
            });
</script>

这是表格

<form method="post" id="contactform" action="/form.php" >
<h5>Category:</h5>
<p><input type="checkbox" name="Location" value="Checked" onclick="toggle_visibility('f1');"> <span class="a1">- Location</span><br><div id="f1" style="display:none">Where are you located?<br /><input name="L2" id="L2" type="text"  /></div></p>

<h5>Name:</h5>
<p><input name="Name" id="v1" type="text" /></p>
<p><div><input name="submit" type="submit" value="Submit"/></div></p>
</form>
4

3 回答 3

3

你的意思是:

rules: {
      fieldName: {
        required: {       
          depends: function() {
            return $("input[name='checkbox']:checked")
          }
        }
      }
    }
于 2012-06-08T05:39:09.827 回答
1

出于某种原因,苏迪尔的回答对我不起作用——我不得不修改它:

rules: {
  fieldName: {
    required: {       
      depends: function(element) {
        return $("input[name='checkbox']:checked").length == 1
      }
    }
  }
}
于 2013-07-29T23:59:54.720 回答
0

苏迪尔的回答是正确的。您所要做的就是在初始化验证时将其用作选项。

var opt = {};//copy from Sudhir's post of course change the field name as required.
$("form").validate(opt);
于 2012-06-08T08:13:26.727 回答