我对JQuery 验证插件的行为有点困惑。
如果我有以下 JQuery:
$('#form1').validate({
/* other validation */
});
$('#txt1').rules("add",
{
required: true,
messages: { required: 'This is required!' }
});
以及以下文本输入,带有 id 但没有 name 属性:
<input type="text" id="txt1"/>
文本框中不会弹出任何必需的消息。
但是,如果我添加一个名称属性:
<input type="text" id="txt1" name="anything"/>
它验证它就好了。
这是为什么?由于我在 ID 选择器上使用了rules("add", rules) 方法,为什么它需要 name 属性才能将规则与元素关联?感谢您的任何见解!