【问题标题】:Validation based on field id rather than field name with jQuery Validation plugin使用 jQuery Validation 插件基于字段 id 而不是字段名称进行验证
【发布时间】:2013-12-01 03:26:46
【问题描述】:

默认情况下,jQuery 验证插件根据字段名称验证表单字段。

我希望插件需要根据字段 id 进行验证。

http://bassistance.de/2013/03/22/release-validation-plugin-1-11-1/

【问题讨论】:

标签: jquery jquery-validate


【解决方案1】:

可以通过添加规则方法:http://jqueryvalidation.org/rules/

【讨论】:

  • 它需要为每个输入字段添加规则。但我需要修改插件以根据 id 而不是名称进行验证。
【解决方案2】:

有一点是肯定的,输入字段的name 必须是唯一的,否则验证将仅适用于单个字段。

您可以使用项目索引(随机或序列号)使名称唯一。

<form id="myform">
    <input type="text" name="bids[spec_attributes][0][name]" /><br/>
    <input type="text"  name="bids[spec_attributes][1][size]"/><br/>
    <input type="text"  name="bids[spec_attributes][2][email]"/><br/>

    <input type="text" name="bids[spec_attributes][10][name]" /><br/>
    <input type="text"  name="bids[spec_attributes][11][size]"/><br/>
    <input type="text"  name="bids[spec_attributes][12][email]"/><br/>
    <input type="submit" />
</form>

在 JS 中

$(document).ready(function () {

    $('#myform').validate();
    $('input[type="text"]').each(function () {
      $(this).rules('add', {
          required: true
      });
    });

});

看看这个小提琴http://jsfiddle.net/cjqvnnho/1/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2020-07-05
    • 2019-10-24
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多