gpt4 book ai didi

javascript - 在 jquery 验证插件中添加自己的规则

转载 作者:行者123 更新时间:2023-12-02 17:37:41 24 4
gpt4 key购买 nike

我有下一个 jquery 代码:

$("input").rules("add", {
required: true,
messages: {
required: 'Please enter your telephone number'
},
errorPlacement: function(error, element) {
console.log('bob');
element.insertBefore(error);
}
});

我正在尝试添加新规则,例如此问题的答案:jquery validation & id's ft numbers

我有这样的html代码:

<form method='post' action='' id='#countersForm'>
<input id="88" class="counter_input active" type="text" enabled="">
<input id="89" class="counter_input active" type="text" enabled="">
</form>

我的问题是:

1) 为什么浏览器尝试验证加载页面上的字段?我不需要这个(页面加载时出现消息 bob。

2) 为什么只有第一个输入字段正在验证?我想验证所有字段。

3)为什么控制台说,该元素未定义?

documentation表示元素参数包含经过验证的元素。 console.log(element) 表示它是未定义的。为什么?

最佳答案

来自documentation :

Read, add and remove rules for an element.

它表示“一个”元素。 $('input') 在您的情况下返回两个元素。

验证插件使用元素的name属性,您的元素没有名称。

您必须在要验证的表单上使用validate()方法初始化插件,例如

$("#myform").validate({ //where my form is the id of your form
rules: {
name: "required", //name is the name of your element
},
messages: {
}
});

关于javascript - 在 jquery 验证插件中添加自己的规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22480910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com