想让 HTML 表单提交什么都不做 [英] Want HTML form submit to do nothing

查看:34
本文介绍了想让 HTML 表单提交什么都不做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望 HTML 表单在提交后什么也不做.

I want an HTML form to do nothing after it has been submitted.

action=""

不好,因为它会导致页面重新加载.

is no good because it causes the page to reload.

基本上,我希望每当按下按钮或有人在输入数据后按 Enter 时调用 Ajax 函数.是的,我可以删除表单标签并添加从按钮的 onclick 事件中调用函数,但我也想要点击输入"功能,而不会变得很黑.

Basically, I want an Ajax function to be called whenever a button is pressed or someone hits Enter after typing the data. Yes, I could drop the form tag and add just call the function from the button's onclick event, but I also want the "hitting enter" functionality without getting all hackish.

推荐答案

通过在从提交按钮调用的 JavaScript 代码中使用 return false;,您可以阻止表单提交.

By using return false; in the JavaScript code that you call from the submit button, you can stop the form from submitting.

基本上,您需要以下 HTML:

Basically, you need the following HTML:

<form onsubmit="myFunction(); return false;">
    <input type="submit" value="Submit">
</form>

然后是支持的 JavaScript 代码:

Then the supporting JavaScript code:

<script language="javascript"><!--
    function myFunction() {
        // Do stuff
    }
//--></script>

如果你愿意,你也可以在某些条件下允许脚本提交表单:

If you desire, you can also have certain conditions allow the script to submit the form:

<form onSubmit="return myFunction();">
    <input type="submit" value="Submit">
</form>

配对:

<script language="JavaScript"><!--
    function myFunction() {
        // Do stuff
        if (condition)
            return true;

        return false;
    }
//--></script>

这篇关于想让 HTML 表单提交什么都不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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