我正在使用腳本驗證表單提交前的幾個表單域。如果驗證失敗,腳本應該返回false。在Linux上,該腳本在FF,Chrome和Opera中運行良好。在Windows上,腳本失敗,表單在Chrome,Safari和IE上提交。 您的想法將不勝感激。表單提交錯誤
$(document).ready(function(){
// Place ID's of all required fields in the array.
required=["formname","formemail"];
email = $("#formemail");
name = $("#formname");
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.";
$("#suggestionform").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");
}
}
if(!/^([a-zA-Z0-9_.-])[email protected](([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())){
email.addClass("needsfilled");
email.val(emailerror);
}
if ("" == name.val()){
name.addClass("needsfilled");
name.val(emptyerror);
}
if($("#link1").val() + $("#link2").val() + $("#link3").val() + $("#link4").val() + $("#textarea1").val() + $("#textarea2").val() == "") {
errornotice.fadeIn(750);
alert("returning fail")
return false;
}
if($(":input").hasClass("needsfilled")){
alert("returning fail here")
return false;
}else{errornotice.hide();return true;}
});
$(":input").focus(function(){
if ($(this).hasClass("needsfilled")){
$(this).val("");
$(this).removeClass("needsfilled");
}
})
});
Version 2
var required=["formname","formemail"];
var email = $("#formemail");
var name = $("#formname");
var errornotice = $("#error");
// The text to show up within a field when it is incorrect
var emptyerror = "Please fill out this field.";
var emailerror = "Please enter a valid e-mail.";
function suggestionSubmit(theform){
required=["formname","formemail"];
email = $("#formemail");
name = $("#formname");
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.";
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");
}
}
if(!/^([a-zA-Z0-9_.-])[email protected](([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())){
email.addClass("needsfilled");
email.val(emailerror);
}
if ("" == name.val()){
name.addClass("needsfilled");
name.val(emptyerror);
}
if($("#link1").val() + $("#link2").val() + $("#link3").val() + $("#link4").val() + $("#textarea1").val() + $("#textarea2").val() == "") {
errornotice.fadeIn(750);
return false
}
if($(":input").hasClass("needsfilled")){
return false;
}
else{errornotice.hide();return true;}
}
你有什麼試圖解決它?哪些字段失敗?他們全部? –
我試過把警報放在腳本會返回false來測試是否真的看到代碼。我在代碼中留下了其中的一些。即使提交表單後,我會看到輸入字段中的錯誤消息(使用會話)以及淡入的錯誤行出現一小段時間,然後表單被提交。這讓我覺得腳本正在做什麼,但表單仍在提交。 – jmreader