jQuery验证功能不起作用

时间:2015-04-01 09:11:57

标签: javascript php jquery html codeigniter

<script type="text/javascript">

    jQuery(function(){

        jQuery("#agree").validate({
            expression: "if (!isChecked(SelfID)) return false; else return true;",
            message: "Please agree the terms and conditions"
        });   
   });

</script>

<form>
<input type="checkbox" value="agree" name="agree" id="agree"  />
<input type="submit">
</form>

1 个答案:

答案 0 :(得分:2)

您需要将验证添加到表单元素,并将所需规则设置为复选框

<form id="myform">
    <input type="checkbox" value="agree" name="agree" id="agree" />
    <input type="submit">
</form>

然后

jQuery(function ($) {
    $("#myform").validate({
        rules: {
            agree: {
                required: true
            }
        },
        messages: {
            agree: "Please agree the terms and conditions"
        }
    });
});

演示:Fiddle