javascript 使用 href 提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20922826/
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
Submit a form using href
提问by user3155047
I have a form
我有一个表格
<form method="post" id="ff">
<input type="name" id="aa" />
<input type="name" id="bb" />
<a href="http://www.example.php "> Submit </a>
</form>
Is it possible to post using the href ? I need to post the datas to href url.Is it possible? Can someone help?
是否可以使用 href 发布?我需要将数据发布到 href url。这可能吗?有人可以帮忙吗?
I dont want to use the normal action=
我不想使用正常的 action=
回答by Bhupendra
You can try this
你可以试试这个
<form method="post" action="http://www.example.php" id="ff">
<input type="name" id="aa" />
<input type="name" id="bb" />
<a href="javascript: submitForm();"> Submit </a>
</form>
<script>
function submitForm(){
$('#ff').submit();
}
</script>
回答by eevaa
Why don't you use
你为什么不使用
<form method="post" id="ff" action="http://www.example.php">
...
</form>
回答by Digital Chris
Yes, you can do this, but it will require JavaScript:
是的,您可以这样做,但它需要 JavaScript:
<a href="javascript:document.getElementById('ff').submit();"> Submit </a>
You could also write more complex JavaScript functions and call that.
您还可以编写更复杂的 JavaScript 函数并调用它。
回答by dev
relace:
相关:
<a href="http://www.example.php "> Submit </a>
with:
和:
<a href="http://www.example.php" onclick="frmsubmit(this);"> Submit </a>
<script type="text/javascript">
function frmsubmit(obj){
var url = obj.href;
document.getElementById('ff').action = url;
document.getElementById('ff').submit();
}
</script>
if you have more than one tag than use id and find it rather than by tag name
如果您有多个标签而不是使用 id 并找到它而不是通过标签名称
回答by Niels Keurentjes
Specify the target URL as the action
attribute of the form
:
指定目标 URL 作为 的action
属性form
:
<form method="post" action="http://www.example.php">
Strictly speaking, what you ask is possible but not pretty:
严格来说,你问的是可能的,但并不漂亮:
<a href="http://www.example.php"
onclick="var e=document.getElementById('ff').action=this.href;e.submit();return false">Submit</a>
I'd go for the action
instead...
我宁愿去action
...