Html 作为提交按钮的标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7340300/
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
a tag as a submit button?
提问by theepan
Hi I am trying to get "a" tag as a submit button. I found a code somewhere in the web. But it didn't work.
嗨,我正在尝试将“a”标签作为提交按钮。我在网上某处找到了一个代码。但它没有用。
<a href="#" onclick="this.form.submit()">Submit</a>
Is there any code for to achieve my need?
是否有任何代码可以满足我的需求?
回答by James Allardice
Give the form
an id
, and then:
给出form
an id
,然后:
document.getElementById("yourFormId").submit();
document.getElementById("yourFormId").submit();
Best practice would probably be to give your link an id
too, and get rid of the event handler:
最佳做法可能是也给您的链接id
,并摆脱事件处理程序:
document.getElementById("yourLinkId").onclick = function() {
document.getElementById("yourFormId").submit();
}
回答by ComFreek
Try this code:
试试这个代码:
<form id="myform">
<!-- form elements -->
<a href="#" onclick="document.getElementById('myform').submit()">Submit</a>
</form>
But users with disabled JavaScript won't be able to submit the form, so you could add the following code:
但是禁用 JavaScript 的用户将无法提交表单,因此您可以添加以下代码:
<noscript>
<input type="submit" value="Submit form!" />
</noscript>
回答by amosrivera
Supposing the form is the direct parent you can do:
假设表单是您可以执行的直接父级:
<a href='#' onclick='this.parentNode.submit(); return false;'>submit</a>
If not you can access through the forms name attribute like this:
如果没有,您可以通过表单名称属性进行访问,如下所示:
<a href='#' onclick='document.forms["myform"].submit(); return false;'>submit</a>
See both examples here: http://jsfiddle.net/WEZDC/1/
在此处查看两个示例:http: //jsfiddle.net/WEZDC/1/
回答by Praveen
Try something like below
尝试像下面这样
<a href="#" onclick="this.forms['formName'].submit()">Submit</a>
回答by Diego Graziano
This is an improve of @ComFreek ans:
这是@ComFreek ans 的改进:
<form id="myform">
<!-- form elements -->
<a href="javascript:;" onclick="document.getElementById('myform').submit()">Submit</a>
</form>
So the will not trigger action and reload your page. Specially if your are developing with a framework with SPA.
因此不会触发操作并重新加载您的页面。特别是如果您正在使用带有 SPA 的框架进行开发。
回答by Thomas H.
in my opinion the easiest way would be somthing like this:
在我看来,最简单的方法是这样的:
<?php>
echo '<a href="link.php?submit='.$value.'">Submit</a>';
</?>
within the "link.php" you can request the value like this:
在“link.php”中,您可以请求这样的值:
$_REQUEST['submit']