我一直试图让我的表单验证(没有jquery验证)。由于某些原因,代码并没有削减它,它实际上导致我的本地环境一些CSS问题。jquery不会显示错误,也不会验证
我真的只是试图验证四个字段,并显示错误。
required = ["id_first_name", "id_last_name", "id_firmbox", "id_job_title"];
errornotice = $("#error");
emptyerror = "Please fill out this field.";
$("#startform").submit(function(){
//Validate required fields
for (i=0;i<required.length;i++) {
var input = $('#'+required[i]);
if ((input.val() == "") || (input.val() == emptyerror)) {
input.addClass("tobefixed");
input.val(emptyerror);
errornotice.fadeIn(750);
} else {
input.removeClass("tobefixed");
}
}
//if any inputs on the page have the class 'tobefixed' the form will not submit
if ($(":input").hasClass("tobefixed")) {
return false;
} else {
errornotice.hide();
requiredeturn true;
}
});
$(":input").focus(function(){
if ($(this).hasClass("tobefixed")) {
$(this).val("");
$(this).removeClass("tobefixed");
}
});
我做了一个fiddle here。也许别人可以发现我做错了什么。 (我对格式化问题表示歉意,我使用的是Ubuntu,一切都会出错)。
非常感谢您的帮助,谢谢!