返回false; onclick提交按钮不起作用

时间:2014-04-24 04:25:24

标签: javascript onclick submit

如果我的代码中有任何错误,我一直试图让我的提交按钮返回false,但它一直把我带到一个破损的页面,我已经尝试在firefox的控制台中,并且没有错误。谢谢 返回false位于底部 代码:

function searchForm(){
//checks all the boxes to see if there are any errors
confirmFName();

confirmLName();

confirmEmail();

confirmAddress1();

confirmAddress2();

confirmCity();

confirmCode();

confirmUName();

confirmPass();

//check to make sure a region is selected
if(nhlForm.regBox == "Choose a Country first", "Choose your Country First"){
    //shows error message
    showMessage(nhlForm.regBox, "error", "*Choose a region");
    showMessage(nhlForm.counBox, "error", "*Choose a country");
}

//checks if checkbox is checked, if not messgae will appear and form will not send
if(nhlForm.agebox.checked == true){
    showMessage(nhlForm.agebox, "bingo", "");
}else{

    //shows error message
    showMessage(nhlForm.agebox, "error", "You must be at least 14 to have an account"); 
}

if(nhlForm.checkbox.checked == true){

    showMessage(nhlForm.checkbox, "bingo", "");
}else{

    //shows error message
    showMessage(nhlForm.checkbox, "error", "You must accept the terms of use to continue"); 
}

//if there are no errors, form will send
if(document.querySelectorAll(".error").length > 0){

//dont submit the form till errors are fixed
return false;
    }
}

1 个答案:

答案 0 :(得分:0)

试试此代码

function searchForm(){
//checks all the boxes to see if there are any errors
confirmFName();

confirmLName();

confirmEmail();

confirmAddress1();

confirmAddress2();

confirmCity();

confirmCode();

confirmUName();

confirmPass();
var flag=true;

//check to make sure a region is selected
if(nhlForm.regBox == "Choose a Country first", "Choose your Country First"){
    //shows error message
    showMessage(nhlForm.regBox, "error", "*Choose a region");
    showMessage(nhlForm.counBox, "error", "*Choose a country");
flag=false;
}

//checks if checkbox is checked, if not messgae will appear and form will not send
if(nhlForm.agebox.checked == true){
    showMessage(nhlForm.agebox, "bingo", "");
}else{

    //shows error message
    showMessage(nhlForm.agebox, "error", "You must be at least 14 to have an account"); 
flag=false;
}

if(nhlForm.checkbox.checked == true){

    showMessage(nhlForm.checkbox, "bingo", "");
}else{

    //shows error message
    showMessage(nhlForm.checkbox, "error", "You must accept the terms of use to continue"); 
flag=false;
}
 return flag;
}
表单标签中的

和onSubmit =“return searchForm()”