gpt4 book ai didi

javascript - jQuery 验证 : Form is Being Validated Twice

转载 作者:行者123 更新时间:2023-11-30 11:56:16 38 4
gpt4 key购买 nike

我有一个表单,我正在使用 jQuery Validate 来验证该表单。我遇到了一个问题。该表单正在验证两次。我的变量“isValid”在

之后两次记录到控制台

触发验证的按钮是类型按钮

<button type="button" name="submit" id="submit" vclass="btn btn-default">Submit</button>

下面是包含要向用户显示的规则和消息的 JavaScript。它还包括按钮的点击事件。

$(function () {
$("#createUser").validate({
rules: {
FirstName: "required",
email: {
required: true,
minlength : 2
},
zipCode: {
required: true,
maxlength : 5
},
Telephone: {
required: function (element) {
return $('#options option:selected').val() != "1";
},
maxlength : 12
},
options: {
required: true,
notEqual: "1"
}
},
messages: {
FirstName: "Please specify your first name",
email: {
required: "This email field is required.",
minlength : "email had to have a min length of 2."
},
zipCode: {
required: "Zip code is a required field.",
maxlength : "zip code cannot be more than 5 characters."
},
Telephone: {
required : "Telephone is a required field.",
maxlength : "Phone number has to have a max length of 12 characters."
},
options: {
required: "Option is a required field",
notEqual: "Select a different option"
}
}
});

jQuery.validator.addMethod("notEqual", function (value, element, param) {
return this.optional(element) || value != param;
}, "Please specify a different (non-default) value");


$('#submit').on('click', function () {
isValid = $('#createUser').valid();
console.log(isValid);
});
});

我发现的大多数与表单验证两次相关的问题都与使用类型提交而不是按钮有关。此外,我不会在我的 JavaScript 文件中的任何地方调用提交。我在这里缺少什么?

最佳答案

如果规则在单独的js中,确保脚本没有被引用两次,

关于javascript - jQuery 验证 : Form is Being Validated Twice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37859584/

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