当“onsubmit”事件调用 javascript 函数时,asp.net 表单未提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8603713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:58:19  来源:igfitidea点击:

asp.net form is not getting submitted when "onsubmit" event calls a javascript function

javascriptasp.netjavascript-eventswebforms

提问by MrClan

the situation is :

情况是:

I want to disable all the controls on the page(possibly using an overlayed div), on the submit of a form, so that the user cannot click on other buttons, till the page is still processing the postback. So, I used the form's "onsubmit" event to call a javascript function that does the same. But now that function is being called, but the form do not get posted at all. I even tried, using "return true;" as the last line of my JavaScript function, but still the form does not get posted.
This is my javascript function :

我想在提交表单时禁用页面上的所有控件(可能使用覆盖的 div),以便用户无法单击其他按钮,直到页面仍在处理回发。因此,我使用表单的“onsubmit”事件来调用执行相同操作的 javascript 函数。但是现在正在调用该函数,但根本没有发布该表单。我什至尝试过,使用“return true;” 作为我的 JavaScript 函数的最后一行,但仍然没有发布表单。
这是我的 javascript 函数:

function DoThis() 
    {
        alert('this form is about to be submitted...');
        return true;
    }

and this is my form tag:

这是我的表单标签:

<form id="form1" runat="server" onsubmit="DoThis();">

What's is wrong here??? Also, let me know if there is some other way that would serve my purpose??? Thanks.

这里有什么问题???另外,让我知道是否有其他方法可以达到我的目的???谢谢。

EDIT : I'm not referring to ajax calls here, since updatepanels already have features to enable this behaviour. I want this functionality for the entire page, even when a button outside an update panel is clicked resulting into full-postback and not just for ajax-calls(since the user normally has sufficient time to click 2 or 3 buttons, by the time the page get's redirected or reloaded).

编辑:我在这里不是指 ajax 调用,因为 updatepanels 已经具有启用此行为的功能。我希望整个页面都具有此功能,即使单击更新面板外的按钮导致完整回发,而不仅仅是 ajax 调用(因为用户通常有足够的时间点击 2 或 3 个按钮,到页面被重定向或重新加载)。

回答by Elias Hossain

Would you please try any below way:

请您尝试以下任何一种方式:

<form id="form1" runat="server" onsubmit="return DoThis();" >

OR

或者

function DoThis() 
    {
        alert('this form is about to be submitted...');
        this.submit();
    }

回答by MrClan

Through a series of hit-n-trials, I finally figured out that I was missing the "return" keyword to add in my form tag. So, I modified my form tag as :

通过一系列的尝试,我终于发现我缺少“return”关键字来添加到我的表单标签中。所以,我将我的表单标签修改为:

<form id="form1" runat="server" onsubmit="return DoThis();">

Silly things eat a lot of time. Probably, in-experience.

傻东西吃了很多时间。可能,在经验中

回答by Aneesh

This is working for me.

这对我有用。

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

    <script>

        function testSubmit(form) {
            form.submit();
        }

    </script>

</head>
<body>

    <form action="http://www.yahoo.com" id="myForm" onsubmit="testSubmit(this)" >
    First name: <input type="text" name="firstname" /><br />
    Last name: <input type="text" name="lastname" />
    <input type="submit"  value="Submit" name="submit" />
</form>

回答by Shady Sherif

of course onsubmitwouldn't work with asp.net.

当然onsubmit不适用于asp.net。

I solved it with very easy way.

我用非常简单的方法解决了它。

if we have such a form

如果我们有这样的表格

<form method="post" name="setting-form" >
   <input type="text" id="UserName" name="UserName" value="" 
      placeholder="user name" >
   <input type="password" id="Password" name="password" value="" placeholder="password" >
   <div id="remember" class="checkbox">
    <label>remember me</label>
    <asp:CheckBox ID="RememberMe" runat="server" />
   </div>
   <input type="submit" value="login" id="login-btn"/>
</form>

You can now catch get that event before the form postback and stop it from postback and do all the ajax you want using this jquery.

您现在可以在表单回发之前捕获该事件并停止回发并使用此 jquery 执行您想要的所有 ajax。

$(document).ready(function () {
            $("#login-btn").click(function (event) {
                event.preventDefault();
                alert("do what ever you want");
            });
 });

回答by Aneesh

in 'DoThis' function add 'this.submit()'

在“DoThis”函数中添加“this.submit()”

function DoThis() { alert('this form is about to be submitted...'); this.submit(); } I havent tested this. Please check.

function DoThis() { alert('this form is about to be submitted...'); this.submit(); } 我还没有测试过这个。请检查。

Are you using any js libraries like JQuery ?

你在使用任何像 JQuery 这样的 js 库吗?