当前位置: 代码迷 >> JavaScript >> onSubmit=“return false”不能遏止表单跳转
  详细解决方案

onSubmit=“return false”不能遏止表单跳转

热度:812   发布时间:2012-08-30 09:55:54.0
onSubmit=“return false”不能阻止表单跳转
<script language="javascript">

function check(){
if(form1.channelName.value==""){
alert("频道名不能为空");
form1.name.focus();
return false;
}
}
</script>
<form action="" name="form1" method="post" onSubmit="return check()">
  ..................................................
  ..................................................
  ..................................................
  ..................................................
  ..................................................

/////////////////////////////////////
当我提交时 提示框出来了(就是alert起到作用了) 但是onSubmit=“return false”没起到作用 表单仍然执行了跳转 请问这是为什么呢


------解决方案--------------------
function check(){
if( document.form1.channelName.value==""){
alert("频道名不能为空");
document.form1.channelName.focus();
return false;
}
}

加document
form1.name.focus(); ->form1.channelName.focus();
------解决方案--------------------
HTML code

<html>

<body>
<script language="javascript">

function check(){
    if(form1.channelName.value==""){
        alert("频道名不能为空");
        form1.channelName.focus();
        return false;
    }
}
</script>
<form action="../" name="form1" method="post" onSubmit="return check();">
    <input type="type" name="channelName" value=""/>
    <input type="submit" name="submit" value="提交"/>
</form>

</body>
</html>

------解决方案--------------------
肯定报错了.....
------解决方案--------------------
你这样写 如果调用check的话 应该永远不会提交吧
因为你没有一个返回 true的地方
------解决方案--------------------
把 check()函数写在 <input type= "submit " name= "submit " value= "提交 " onclick= "return check() "/>
------解决方案--------------------
JScript code


// 给你的建议,因为程序的错误永远不会影响submit
function check(){
    var checked = false;

    try {
        if(form1.channelName.value==""){
            alert("频道名不能为空");
            form1.name.focus();
            return false;
        } else {
            checked = true;
        }
    } catch(e) {
        checked = false;
    }


    return checked ? true : false;
}

------解决方案--------------------
<script language="javascript">

function check(){
if(form1.channelName.value==""){
alert("频道名不能为空");
return;
form1.name.focus();
$("form1").submit();
}
}
</script>
<form action="form1" id="" name="form1" method="post" onSubmit="check()">
------解决方案--------------------
你只有个一个if(),而没有对应的else,即使没有你也应该有个默认的return才对
------解决方案--------------------
onSubmit?要大写吗?
------解决方案--------------------

<html>

<body>
<script language="javascript">
  相关解决方案