简体   繁体   English

使用jQuery Validation插件验证引导表单时,是否需要使用名称属性?

[英]Do I need to use name attributes when validating a bootstrap form with jQuery Validation plugin?

Still kind of new and I just started using bootstrap but basically when using stock HTML I see name attributes being used like so 还是一种新东西,我刚开始使用引导程序,但是基本上在使用股票HTML时,我看到像这样使用name属性

<label for="email" class="label">E-mail Address</label>
    <input name="email" type="text" id="email"> 

but with bootstrap there is typically no name attr used. 但是使用引导程序通常不会使用名称attr。

     <div class="form-group">
        <label for="inputPassword" class="col-lg-2 control-label">Password</label>
        <div class="col-lg-5">
          <input type="password" class="form-control" id="inputPassword-" placeholder="Password">
        </div>
      </div>

I guess i'm just confused where the jquery validator is pulling information from. 我想我只是在混淆jquery验证程序从中提取信息。 Does the email refer to the name attr or the label for? 电子邮件中是否引用了名称attr或标签?

    rules: {
      email: {
      required: true,
      email: true
    },

Adding this answer to clarify some ambiguous and misleading information posted within another's answer. 添加此答案以澄清发布在他人答案中的一些含糊不清和误导性信息。

Quote Title: 引用标题:

"Do I need to use name attributes when validating a bootstrap form with jQuery Validation plugin?" “使用jQuery Validation插件验证引导表单时,我需要使用名称属性吗?”

It does not matter what kind of form. 哪种形式都没有关系。 When using the jQuery Validation plugin, any data input elements that are to be validated must each contain a unique name attribute, period... no workarounds, no exceptions. 使用jQuery Validation插件时, 要验证的任何数据输入元素都必须包含唯一的name属性,句点...没有解决方法,也没有例外。 (The name attribute is the only way the jQuery Validation plugin can keep track of the data input elements being validated.) name属性是jQuery Validation插件可以跟踪正在验证的数据输入元素的唯一方法。)

This means that you cannot get around this requirement by using the .rules('add') method. 这意味着您无法使用.rules('add')方法来解决此要求。 I've seen this touted more than once on SO as a proposed workaround for elements without a name attribute. 我已经在SO上多次吹捧它作为不带name属性的元素的建议解决方法。 This will not work. 这是行不通的。 No matter how rules are declared , the data input elements must still have a name attribute. 无论如何声明规则 ,数据输入元素都必须仍然具有name属性。

Demo without name attributes (broken validation): http://jsfiddle.net/L3u5n/ 没有name属性的演示(无效验证): http : //jsfiddle.net/L3u5n/

Identical demo, but with name attributes (working validation): http://jsfiddle.net/L3u5n/1/ 相同的演示,但具有name属性(工作验证): http : //jsfiddle.net/L3u5n/1/


See the " Markup recommendations " section of the " Reference docs " page . 请参阅参考文档 ”页面的“ 标记建议 ”部分。

"The name attribute is ' required ' for input elements, the validation plugin doesn't work without it." “输入元素的name属性为' required ',如果没有它,验证插件将无法工作 。”

The validator uses the name attribute to find the field, as all form elements must have a name attribute. 验证器使用name属性来查找字段,因为所有表单元素都必须具有name属性。

The second block of HTML code you've posted is not valid for that reason. 因此,您发布的第二段HTML代码无效。

name is must in the form element and you need it for jQuery validator too name必须在form元素中, jQuery validator也需要它


you can also use jQuery validator like this 您也可以像这样使用jQuery validator

.rules( “add”, rules ) .rules(“添加”,规则)

 $("el").rules("add", { required: true, email: true }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM