【问题标题】:Checkbox Using "fieldname[ ]" breaks jquery validation使用“fieldname []”的复选框会破坏 jquery 验证
【发布时间】:2012-03-19 05:50:41
【问题描述】:

所以我有一个需要验证的表单。表单验证的其余部分有效,但是当我使用 php 属性 [] 添加复选框组以将复选框形成数组时。复选框不再适用于我的验证脚本。

$("form[name='violationsForm']").validate({
    ignore: ":hidden,[name='']", // do not validate form fields in invisible sections
    errorPlacement: function(error, element) {
         $(element).attr("class","error");
         error.insertAfter(element);
     },
    rules: {
         owner_address: { required: true, minlength: 5 }
        ,owner_county: { required: true, minlength: 1 }
        ,date1: { required: true, minlength: 2 }
        ,time1: { required: true, minlength: 2 }
        ,timeframe1: { required: true, minlength: 2 }
        ,violation[]: { required: true}
    }

});

添加 ,violation[]: { required: true} 会破坏整个验证脚本。 以下是我的参考表单输入。

<li>
<label><strong>Type of Violation (required):</strong></label><br />
<label>type 1 <input type="checkbox" id="violation" name="violation[]" value="1"     
 title="Please choose at least ONE violation."/></label><br />
 <label>type 2 <input type="checkbox" id="violation" name="violation[]" value="2" />  
</label><br />
<label>type 3 <input type="checkbox" id="violation" name="violation[]" value="3" />
</label><br />
<label>type 4 <input type="checkbox" id="violation" name="violation[]" value="4" />
</label><br />
<label>type 5 <input type="checkbox" id="violation" name="violation[]" value="5" />  
</label><br />
<label>Other <input type="checkbox" id="violation_0" name="violation[]" value="other" />
</label><br />
</li>

由于我使用的是 php,因此我更愿意将 [] 附加到我的输入“名称”值。有什么想法吗?

谢谢!

【问题讨论】:

    标签: php jquery forms jquery-validate


    【解决方案1】:

    用引号括起来:

    "violation[]"
    

    【讨论】:

      【解决方案2】:

      特殊字符需要在 jQuery 选择器中使用双反斜杠进行转义。当您为规则对象创建键时,它们将被用作选择器。

      选择器 API 的顶部包含所有转义信息 http://api.jquery.com/category/selectors/

      【讨论】:

        猜你喜欢
        • 2015-12-09
        • 1970-01-01
        • 1970-01-01
        • 2016-02-18
        • 1970-01-01
        • 1970-01-01
        • 2014-09-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多