如何在提交按钮 onclick 事件中取消表单提交?

发布于 2024-10-03 13:33:40 字数 280 浏览 4 评论 0原文

我正在开发 ASP.net Web 应用程序。

我有一个带有提交按钮的表单。提交按钮的代码类似于

我想写如下内容:

function btnClick() {
    if (!validData())
        cancelFormSubmission();
}

我该怎么做?

I'm working on an ASP.net web application.

I have a form with a submit button. The code for the submit button looks like <input type='submit' value='submit request' onclick='btnClick();'>.

I want to write something like the following:

function btnClick() {
    if (!validData())
        cancelFormSubmission();
}

How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

苏璃陌 2024-10-10 13:33:41
<input type='button' onclick='buttonClick()' />

<script>
function buttonClick(){
    //Validate Here
    document.getElementsByTagName('form')[0].submit();
}
</script>
<input type='button' onclick='buttonClick()' />

<script>
function buttonClick(){
    //Validate Here
    document.getElementsByTagName('form')[0].submit();
}
</script>
薆情海 2024-10-10 13:33:40

您最好这样做...

<form onsubmit="return isValidForm()" />

如果 isValidForm() 返回 false,则您的表单不会提交。

您可能还应该将事件处理程序从内联中移出。

document.getElementById('my-form').onsubmit = function() {
    return isValidForm();
};

You are better off doing...

<form onsubmit="return isValidForm()" />

If isValidForm() returns false, then your form doesn't submit.

You should also probably move your event handler from inline.

document.getElementById('my-form').onsubmit = function() {
    return isValidForm();
};
静谧幽蓝 2024-10-10 13:33:40

将您的输入更改为:

<input type='submit' value='submit request' onclick='return btnClick();'>

并在函数中返回 false

function btnClick() {
    if (!validData())
        return false;
}

Change your input to this:

<input type='submit' value='submit request' onclick='return btnClick();'>

And return false in your function

function btnClick() {
    if (!validData())
        return false;
}
亚希 2024-10-10 13:33:40

您需要更改

onclick='btnClick();'

onclick='return btnClick();'

cancelFormSubmission();

就是说

return false;

,我会尽量避免使用内部事件属性,以支持 unobtrusive JS 使用一个库(例如 YUI 或 jQuery),该库具有良好的事件处理 API 并与真正重要的事件相关联(即表单的提交事件而不是按钮的单击事件)。

You need to change

onclick='btnClick();'

to

onclick='return btnClick();'

and

cancelFormSubmission();

to

return false;

That said, I'd try to avoid the intrinsic event attributes in favour of unobtrusive JS with a library (such as YUI or jQuery) that has a good event handling API and tie into the event that really matters (i.e. the form's submit event instead of the button's click event).

雪若未夕 2024-10-10 13:33:40

有时 onsubmit 不适用于 ASP.NET。

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

如果我们有这样一个表单,

<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>

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

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

Sometimes onsubmit wouldn't work with 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.

$(document).ready(function () {
            $("#login-btn").click(function (event) {
                event.preventDefault();
                alert("do what ever you want");
            });
 });
妄断弥空 2024-10-10 13:33:40

您应该将类​​型从 submit 更改为 button

<input type='button' value='submit request'>

而不是

<input type='submit' value='submit request'>

在 JavaScript 中获取按钮的名称,并将您想要的任何操作与它对

var btn = document.forms["frm_name"].elements["btn_name"];
btn.onclick = function(){...};

我有用的操作 关联起来
希望有帮助。

you should change the type from submit to button:

<input type='button' value='submit request'>

instead of

<input type='submit' value='submit request'>

you then get the name of your button in javascript and associate whatever action you want to it

var btn = document.forms["frm_name"].elements["btn_name"];
btn.onclick = function(){...};

worked for me
hope it helps.

无法回应 2024-10-10 13:33:40

这是一个非常古老的线程,但肯定会引起注意。因此,请注意,所提供的解决方案不再是最新的,现代 Javascript 更好。

<script>
document.getElementById(id of the form).addEventListener(
    "submit",
    function(event)
    {
        if(validData() === false)
        {
            event.preventDefault();
        }
    },
    false
);

该表单接收一个监视提交的事件处理程序。如果调用的函数 validData(此处未显示)返回 FALSE,则调用方法 PreventDefault,该方法会抑制表单的提交,并且浏览器返回到输入。否则,表格将照常发送。

PS 这也适用于属性onsubmit。那么匿名函数 function(event){...} 必须位于表单的属性 onsubmit 中。这并不是真正的现代,您只能使用一个事件处理程序来进行提交。但您不必创建额外的 javascript。另外,它可以直接在源代码中指定为表单的属性,而无需等到表单集成到 DOM 中。

This is a very old thread but it is sure to be noticed. Hence the note that the solutions offered are no longer up to date and that modern Javascript is much better.

<script>
document.getElementById(id of the form).addEventListener(
    "submit",
    function(event)
    {
        if(validData() === false)
        {
            event.preventDefault();
        }
    },
    false
);

The form receives an event handler that monitors the submit. If the there called function validData (not shown here) returns a FALSE, calling the method PreventDefault, which suppresses the submit of the form and the browser returns to the input. Otherwise the form will be sent as usual.

P.S. This also works with the attribute onsubmit. Then the anonymus function function(event){...} must in the attribute onsubmit of the form. This is not really modern and you can only work with one event handler for submit. But you don't have to create an extra javascript. In addition, it can be specified directly in the source code as an attribute of the form and there is no need to wait until the form is integrated in the DOM.

∝单色的世界 2024-10-10 13:33:40

您需要返回 false;

<input type='submit' value='submit request' onclick='return btnClick();' />

function btnClick() {
    return validData();
}

You need to return false;:

<input type='submit' value='submit request' onclick='return btnClick();' />

function btnClick() {
    return validData();
}
死开点丶别碍眼 2024-10-10 13:33:40

使用 JQuery 更加简单:适用于 Asp.Net MVC 和 Asp.Core

<script>
    $('#btnSubmit').on('click', function () {

        if (ValidData) {
            return true;   //submit the form
        }
        else {
            return false;  //cancel the submit
        }
    });
</script>

With JQuery is even more simple: works in Asp.Net MVC and Asp.Core

<script>
    $('#btnSubmit').on('click', function () {

        if (ValidData) {
            return true;   //submit the form
        }
        else {
            return false;  //cancel the submit
        }
    });
</script>
多孤肩上扛 2024-10-10 13:33:40

为什么不将提交按钮更改为常规按钮,并在单击事件中,如果通过验证测试则提交表单?

例如

<input type='button' value='submit request' onclick='btnClick();'>

function btnClick() { 
    if (validData()) 
        document.myform.submit();
} 

Why not change the submit button to a regular button, and on the click event, submit your form if it passes your validation tests?

e.g

<input type='button' value='submit request' onclick='btnClick();'>

function btnClick() { 
    if (validData()) 
        document.myform.submit();
} 
山有枢 2024-10-10 13:33:40

您需要onSubmit。不是 onClick 否则有人只需按 Enter 键即可绕过您的验证。至于取消。你需要返回 false。代码如下:

<form onSubmit="return btnClick()">
<input type='submit' value='submit request'>

function btnClick() {
    if (!validData()) return false;
}

Edit onSubmit 属于表单标记。

You need onSubmit. Not onClick otherwise someone can just press enter and it will bypass your validation. As for canceling. you need to return false. Here's the code:

<form onSubmit="return btnClick()">
<input type='submit' value='submit request'>

function btnClick() {
    if (!validData()) return false;
}

Edit onSubmit belongs in the form tag.

国产ˉ祖宗 2024-10-10 13:33:40

很简单,返回false即可;

下面的代码使用 jquery 在提交按钮的 onclick 中进行。

if(conditionsNotmet)
{
return false;
}

It's simple, just return false;

The below code goes within the onclick of the submit button using jquery..

if(conditionsNotmet)
{
return false;
}
你的呼吸 2024-10-10 13:33:40

使用 onclick='return btnClick();'

function btnClick() {
    return validData();
}

use onclick='return btnClick();'

and

function btnClick() {
    return validData();
}
于我来说 2024-10-10 13:33:40
function btnClick() {
    return validData();
}
function btnClick() {
    return validData();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文