javascript跳过验证表单

时间:2014-04-03 04:24:27

标签: javascript html html5 forms validation

我有html在表单后运行javascript,

form name="mortgage" method="post" action=" "onsubmit="return completeFormValidation();">

用于验证的javascript代码,

function completeFormValidation() {

location();


} // End of completeFormValidation

function location(){



var numradio = document.mortgage.propLocation.length;
var selected="";

for(var i=0; i < numradio; i++){

    if(document.mortgage.propLocation[i].checked == true){
        selected += "Selected radio item " + i;

    }
}

if(selected == ""){

    document.getElementById("reserved").innerHTML = "<p> none radio selected </P>";
    return false;

}

}

代码在Dream weaver中运行得非常好,但是在浏览器中似乎没有工作,即使没有选择单选按钮也会提交。请帮我谢谢。

3 个答案:

答案 0 :(得分:3)

因为你没有返回&#34; FALSE / TRUE&#34;来自 completeFormValidation 函数的值..并且更改函数的名称位置由JavaScript保留。

检查jsfiddle

function completeFormValidation() {

return my_location();

}

总是更好地从位置返回值 true或false

您可以修改 my_location(),如下所示

function my_location(){

var numradio = document.mortgage.propLocation.length;
var selected="";

for(var i=0; i < numradio; i++){

    if(document.mortgage.propLocation[i].checked == true){
        selected += "Selected radio item " + i;

    }
}

if(selected == ""){

    document.getElementById("reserved").innerHTML = "<p> none radio selected </P>";
    return false;

}
else{
    return true;
}

}

答案 1 :(得分:0)

尝试替换

function completeFormValidation() {

location();

} // End of completeFormValidation

通过

function completeFormValidation() {

return location();

} // End of completeFormValidation

答案 2 :(得分:0)

重命名location功能。 locationwindow对象的原生属性,无法重新定义。