想让HTML表单提交什么都不做 [英] Want html form submit to do nothing
问题描述
我要被提交后,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.
基本上我想只要一个按钮为pressed或有人打进入输入一次数据之后一个ajax函数被调用。是的,我可以在表单标签,并添加只是调用从按钮的onclick事件的功能,但我也希望没有得到所有的hackish的按下回车键功能。
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.
推荐答案
通过使用返回false;
在您提交按钮调用JavaScript,你可以停止从提交表单。
By using return false;
in the javascript 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:
<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屋!