JQuery-Validation-对选定的ID使用rules方法,为什么必须在元素上存在name属性? [英] JQuery-Validation - using rules method on selected ID, why does the name attribute have to be present on the element?

查看:238
本文介绍了JQuery-Validation-对选定的ID使用rules方法,为什么必须在元素上存在name属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 JQuery验证插件的行为感到困惑.

如果我有以下JQuery:

If I have the following JQuery:

$('#form1').validate({
    /* other validation */
});

$('#txt1').rules("add",
{
    required: true,
    messages: { required: 'This is required!' }
});

以及以下带有id但没有名称属性的文本输入:

and the following text input, with an id but no name attribute:

<input type="text" id="txt1"/>

在文本框中没有弹出任何不需要的消息.

No required message pops up on the text box.

但是,如果我添加了name属性:

However, if I add a name attribute:

<input type="text" id="txt1" name="anything"/>

它可以很好地验证它.

那是为什么?由于我使用的是 rules("add",rules)方法在ID选择器上,为什么需要存在name属性才能将规则与元素相关联?感谢您提供任何见识!

Why is that? Since I'm using the rules("add", rules) method on an ID selector, why does it need the name attribute present in order to associate rules to the element? Thanks for any insight!

推荐答案

首先在元素上需要一个name属性,因为这是jQuery验证内部使用的每个字段的键,其次是因为name属性input元素上必填,以确保页面验证为指定的DOCTYPE.

A name attribute is required on the element firstly because that's what jQuery validate uses internally as a key for each field, and secondly because the name attribute is required on input elements to ensure the page validates to the specified DOCTYPE.

这篇关于JQuery-Validation-对选定的ID使用rules方法,为什么必须在元素上存在name属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆