gpt4 book ai didi

javascript - JSHint "not defined"错误

转载 作者:行者123 更新时间:2023-11-28 08:55:07 25 4
gpt4 key购买 nike

我有一个用于验证联系表单的函数,但是当我通过 JSHint 运行它时,我收到了大量“未定义”错误。我是一个 jquery 菜鸟,我不确定这些应该如何定义。

(function ($, document, undefined) {
$(document).ready(function(){

// Place ID's of all required fields here.
required = ["name", "email", "message"];
// If using an ID other than #email or #error then replace it here
email = $("#email");
errornotice = $("#error");
// The text to show up within a field when it is incorrect
emptyerror = "Please fill out this field.";
emailerror = "Please enter a valid e-mail.";

$("#theform").submit(function(){
//Validate required fields
for (i=0;i<required.length;i++) {
var input = $('#'+required[i]);
if ((input.val() == "") || (input.val() == emptyerror)) {
input.addClass("needsfilled");
input.val(emptyerror);
errornotice.fadeIn(750);
} else {
input.removeClass("needsfilled");
}
}
// Validate the e-mail.
if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
email.addClass("needsfilled");
email.val(emailerror);
}

//if any inputs on the page have the class 'needsfilled' the form will not submit
if ($(":input").hasClass("needsfilled")) {
return false;
} else {
errornotice.hide();
return true;
}
});

// Clears any fields in the form when the user clicks on them
$(":input").focus(function(){
if ($(this).hasClass("needsfilled") ) {
$(this).val("");
$(this).removeClass("needsfilled");
}
});
});
})(jQuery, document);

这里有几个例子:

来自表单数据 - 未定义“必需”。

required = ["name", "email", "message"];

“i”未定义。

for (i=0;i<required.length;i++) {

并且“电子邮件”未定义。

if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {

最佳答案

您应该使用“var”定义变量:

var required = ["name", "email", "message"];
for(var i...

关于javascript - JSHint "not defined"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18612037/

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