Javascript 想要html表单提交什么都不做
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3384960/
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
Want html form submit to do nothing
提问by Steven Biggums
I want an html form to do nothing after it has been submitted.
我想要一个 html 表单在提交后什么都不做。
action=""
is no good because it causes the page to reload.
不好,因为它会导致页面重新加载。
Basically i want an ajax function to be called whenever a button is pressed or someone hits "enter" after typing it 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.
基本上,我希望在按下按钮或有人在键入数据后点击“输入”时调用 ajax 函数。是的,我可以删除表单标记并添加仅从按钮的 onclick 事件调用该函数,但我也希望“点击输入”功能而不会变得很黑。
回答by Jeffrey Blake
By using return false;in the javascript that you call from the submit button, you can stop the form from submitting.
通过return false;在从提交按钮调用的 javascript 中使用,您可以停止提交表单。
Basically, you need the following HTML:
基本上,您需要以下 HTML:
<form onsubmit="myFunction(); return false;">
<input type="submit" value="Submit">
</form>
Then the supporting javascript:
然后是支持的javascript:
<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>
Paired with:
配对:
<script language="JavaScript"><!--
function myFunction() {
//do stuff
if (condition)
return true;
return false;
}
//--></script>
回答by simple
<form id="my_form" onsubmit="return false;">
is enough ...
足够 ...
回答by simple
It also works:
它也有效:
<form id='my_form' action="javascript:myFunction(); return false;">
回答by Keyur Shah
You can use the following HTML
您可以使用以下 HTML
<form onSubmit="myFunction(); return false;">
<input type="submit" value="Submit">
</form>
回答by ZX12R
How about
怎么样
<form id="my_form" onsubmit="the_ajax_call_function(); return false;">
......
</form>
回答by Matt Phillips
use jquery. Maybe put a div around your elements from the form. Then use preventDefault() and then make your ajax calls
使用jQuery。也许在表单中的元素周围放置一个 div。然后使用 preventDefault() 然后进行 ajax 调用
回答by Ross
As part of the button's onclick event return false which will stop the form from submitting.
作为按钮的 onclick 事件的一部分,返回 false 将阻止表单提交。
ie:
IE:
function doFormStuff() {
// ajax function body here
return false;
}
And just call that function on submit button click. There are many different ways.
只需在单击提交按钮时调用该函数。有很多不同的方式。
回答by Junjie Li
<form action='javascript:functionWithAjax("search");'>
<input class="keyword" id="keyword" name="keyword" placeholder="input your keywords" type="search">
<i class="iconfont icon-icon" onclick="functionWithAjax("search");"></i>
</form>
<script type="text/javascript">
function functionWithAjax(type) {
// ajax action
return false;
}
</script>
回答by Ali Jamal
<form onsubmit="return false;">

