javascript 你如何做一个表单的动作为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4737559/
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
How do you do the action for a form be null
提问by norris1023
When defining a <form>, it is possible to set the actiontag.
定义 a 时<form>,可以设置action标签。
How can I set it to null (#), so that it will not actually change the page?
如何将其设置为 null (#),以便它实际上不会更改页面?
回答by CodeVirtuoso
A bit hard to understand but I think what you want is this:
有点难以理解,但我认为您想要的是:
<form action="#">
<!-- code here -->
</form>
回答by drudge
A <form>tag without an actionattribute will send the data to the page it was submitted from.
<form>没有action属性的标签会将数据发送到它提交的页面。
You only need to use the actionattribute if you're sending the form data to a different page.
action如果您将表单数据发送到不同的页面,您只需要使用该属性。
<form method="GET">
<input type="text" size="10" name="input" value="default">
<input type="submit" value="Submit">
</form>
(NOTE: This does not work on the file:///protocol handler.)
(注意:这不适用于file:///协议处理程序。)
回答by Shaan
You can set form action to null in the following way. This worked for me.
您可以通过以下方式将表单操作设置为 null。这对我有用。
< form action ='' >
回答by DevOops
For anyone else reading this, it is usually better to have your input or button object return false; when it's clicked. This will make sure the form doesn't follow an action, and instead stays on the page. It would be similar conceptually to what is being asked, as in it will stay on the current page.
对于阅读本文的其他人,通常最好让您的输入或按钮对象返回 false;当它被点击。这将确保表单不会跟随操作,而是停留在页面上。它在概念上与所询问的内容相似,因为它将保留在当前页面上。
This let's you handle submit options in javascript.
这让您可以在 javascript 中处理提交选项。
Here's an example of how you would submit an email address to your API route, alerting the user what is returned by the API call:
下面是一个示例,说明如何向 API 路由提交电子邮件地址,提醒用户 API 调用返回的内容:
HTML:
HTML:
<form>
<input type="text" name="email">
<button onclick="do_something(); return false;"
</form>
JAVASCRIPT:
爪哇脚本:
var do_something = function(){
$.ajax({
url: "http://myapi.com/route",
type: 'POST',
data: $('form').serialize(),
context: document.body
}).done(function(data) {
alert(data);
});
};

