onsubmit方法不会停止提交 [英] onsubmit method doesn't stop submit

查看:168
本文介绍了onsubmit方法不会停止提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 onsubmit 不起作用。我的想法是放置一些必填字段,为了达到这个目的,我在HTML中的一个表单中使用 onsubmit 方法来调用JavaScript函数。



这个想法是,如果所有必填字段都已填充,那么javascript函数将返回 true ,并且它会移至页面 /control/Cadastro.php 。否则,如果任何必填字段为空,它将返回 false ,并且它不会移动到页面 /control/Cadastro.php

不幸的是,该函数确实返回 false ,如果所有的必填字段未按预期填充,但它仍会移至页面 /control/Cadastro.php ,即使它不应该。



我将删除一些代码以使我的观点可以被感知。

 < !DOCTYPE html> 
< html>
< head>
< script>
函数ValidateRequiredFields()
{
var message = new String('\ nCamposobrigatórios:\\\
');
var flag = new Boolean(1);
var x = document.forms [theForm] [nr_processoCA] .value;
if(x == null || x ==){
message + ='\\\
Nºdo processo \\\
';
flag = new Boolean(0);
}
if(flag == false){
alert(message);
}
返回标志;
}
< / script>
< / head>
< body>
< form name =theFormonsubmit =return ValidateRequiredFields()method =postaction =../ control / Cadastro.php>
Nº做Processo:< br>
< input type =textname =nr_processoCAclass =input-xlarge>
< br>
< div class =row-fluidstyle =text-align:center;>
< input type =submitclass =btn btn-primary btn-largevalue =Gravar>
< / div>
< / form>
< / body>
< / html>


解决方案

您应该使用 preventDefault 在您的onsubmit函数中。我修改了你的代码:

 函数ValidateRequiredFields()
{
var message = new String('\\ \\ nCamposobrigatórios:\\\
');
var flag = new Boolean(1);


var x = document.forms [theForm] [nr_processoCA] .value;
if(x == null || x ==){
message + ='\\\
Nºdo processo \\\
';
flag = new Boolean(0);


if(flag == false){
if(event.preventDefault){
event.preventDefault();
} else {
event.returnValue = false; //对于IE,因为不支持preventDefault;
}
alert(message);
}

返回标志;
}

DEMO


My onsubmit is not working. My idea was to put some mandatory fields and, in order to achieve that, I was using the onsubmit method inside a form in HTML that called a JavaScript function.

The idea was if all the mandatory fields were filled, the javascript function would return true and it would move on to page /control/Cadastro.php. Otherwise, if any mandatory field was empty, it would return false and it wouldn't move to page /control/Cadastro.php, staying in the current page until true.

Unfortunately, the function does return false if all the mandatory fields are not filled, as expected, but it still moves to page /control/Cadastro.php, even if it shouldn't.

I'm going to cut off some code to make my point of view perceptible.

<!DOCTYPE html>
<html>
    <head>
        <script>
            function ValidateRequiredFields()
            {
                var message = new String('\nCampos obrigatórios:\n');
                var flag=new Boolean(1);
                var x=document.forms["theForm"]["nr_processoCA"].value;
                if (x==null || x==""){
                    message += '\nNº do processo\n'; 
                    flag = new Boolean(0);
                } 
                if (flag == false){
                    alert(message);
                }
                return flag;    
            }
        </script>
    </head>
    <body>
        <form name="theForm" onsubmit="return ValidateRequiredFields()" method="post" action="../control/Cadastro.php"> 
            Nº do Processo: <br>
            <input type="text" name="nr_processoCA" class="input-xlarge">
            <br>
            <div class="row-fluid" style="text-align:center;">
                <input type="submit" class="btn btn-primary btn-large" value="Gravar">
            </div>   
         </form>
    </body>
</html>

解决方案

You should use preventDefault inside your onsubmit function. I modified your code:

function ValidateRequiredFields()
{
    var message = new String('\nCampos obrigatórios:\n');
    var flag=new Boolean(1);


    var x=document.forms["theForm"]["nr_processoCA"].value;
    if (x==null || x==""){
        message += '\nNº do processo\n'; 
        flag = new Boolean(0);
    }

    if (flag == false){
        if(event.preventDefault){
            event.preventDefault();
        }else{
            event.returnValue = false; // for IE as dont support preventDefault;
        }
        alert(message);
    }

    return flag;
}

DEMO

这篇关于onsubmit方法不会停止提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆