【问题标题】:jQuery: selecting elements according to the beginning and the end of the name attributejQuery:根据name属性的开头和结尾选择元素
【发布时间】:2011-07-26 03:05:00
【问题描述】:

我有这个代码:

  <script>
  $(document).ready(function(){
  $('form').validate({

    //rules: { 'input_name': { email: true} },
    rules: { 'name^=input', 'name$=jander': { email: true} },
    messages: { 'input_name': "Jar" },

    errorPlacement: function(error, element) {
     error.insertBefore(element);
    },
    debug:true

    })

  });
  </script>

 </head> 

  <body>
<div id="jar">fasdf</div>

<form id="clar">
<p id="my_paragraph">Escribe ALGO para que haga la validacion de email</p>
<input name='input_jander' type="text" ">
<input type="submit" >

</form>

如您所见,我尝试将验证规则应用于名称属性以“input”以“jander”结尾的所有输入字段,但它不起作用(没有显示错误信息)..

【问题讨论】:

    标签: jquery jquery-selectors jquery-validate


    【解决方案1】:

    只需提供您想要针对某个类的输入并以这种方式定位它们会容易得多。为什么要让自己变得更难

    【讨论】:

      【解决方案2】:

      这些键只是映射到表单的name 属性。

      但是,我以前遇到过一次这个问题,我就这样解决了......

      var rules = {},
          messages = {},
          inputs = $(':input[name^="input"]').filter(':input[name$="jander"]');
      
      inputs.each(function() {
         var name = $(this).attr('name');
         rules[name] = { email: true };
         messages[name] = { email: 'Type a proper email, mate!' };
      });
      
      $('form').validate({
          'rules': rules,
          'messages': messages
      });
      

      jsFiddleinput 元素被匹配。

      尽管您可能应该为这些元素想出一个更合理的标识符。

      【讨论】:

        猜你喜欢
        • 2013-11-15
        • 1970-01-01
        • 2023-04-07
        • 2011-05-16
        • 1970-01-01
        • 2010-11-02
        • 2012-02-12
        • 2010-09-18
        相关资源
        最近更新 更多