Javascript JSHint“;“未定义”;错误
我使用这个函数来验证一个联系人表单,但是当我通过JSHint运行它时,会出现大量“未定义”错误。我是一个jquerynoob,我不知道这些应该如何定义Javascript JSHint“;“未定义”;错误,javascript,jquery,debugging,jshint,Javascript,Jquery,Debugging,Jshint,我使用这个函数来验证一个联系人表单,但是当我通过JSHint运行它时,会出现大量“未定义”错误。我是一个jquerynoob,我不知道这些应该如何定义 (function ($, document, undefined) { $(document).ready(function(){ // Place ID's of all required fields here. required = ["name", "email", "message"]; // If usin
(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"];
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
声明的变量显然意味着使用的是未声明的变量。在某些情况下,这样做是为了生成具有全局范围的变量。当然,这可以(以更简洁的方式)通过使用var email在所有闭包之外的某处声明变量来实现代码>
var required = ["name", "email", "message"];
for(var i...