javascript 我想要一个锚应该像输入类型提交按钮一样
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6086816/
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
i want a anchor should act like and input type submit button
提问by Mossawir Ahmed
i want a anchor should act like and input type submit button. i am using a jquery plugin library that actually uses input type submit but i have styled my buttons on anchors. i dont want to use
我想要一个锚应该像和输入类型提交按钮一样。我正在使用一个 jquery 插件库,它实际上使用输入类型提交,但我已经在锚点上设置了按钮的样式。我不想使用
<input type="button">
or
或者
<input type="submit">
i want to use anchors such as
我想使用锚点,例如
<a href="javascript to submit the form" ></a>
and here is my jquery code where i want to use
这是我要使用的 jquery 代码
{
var submit = $('<button type="submit" />');
submit.html(settings.submit);
}
$(this).append(submit);
}
if (settings.cancel) {
/* if given html string use that */
if (settings.cancel.match(/>$/)) {
var cancel = $(settings.cancel);
/* otherwise use button with given string as text */
} else {
var cancel = $('<button type="cancel" />');
how to use anchors instead of button.
如何使用锚点而不是按钮。
回答by mazlix
If you want an anchor tag to act like a button just do this
如果你想让一个锚标签像一个按钮一样,就这样做
<!--YOUR FORM-->
<form id="submit_this">.....</form>
<a id="fakeanchor" href="#"></a>
<script>
$("a#fakeanchor").click(function()
{
$("#submit_this").submit();
return false;
});
</script>
回答by T.J. Crowder
Since you're using jQuery, just use $()
to select the form
element, and call submit
on it; hook all this up to the anchor via $()
to find the anchor and click
to hook up the handler:
由于您使用的是 jQuery,因此只需使用$()
来选择form
元素,然后调用submit
它即可;将所有这些连接到锚点通过$()
找到锚点并click
连接处理程序:
$("selector_for_the_anchor").click(function() {
$("selector_for_the_form").submit();
return false;
});
Probably best to return false;
to cancel the click on the anchor.
可能最好return false;
取消对锚点的点击。
Off-topic: But note that this makes your page completely unusable without JavaScript, as well as making it confusing even for JavaScript-enabled browsers employed by users requiring assistive technologies (screenreaders, etc.). It makes the markup completely un-semantic. But since you'd said quite clearly that this was what you wanted to do...
题外话:但请注意,这会使您的页面在没有 JavaScript 的情况下完全无法使用,并且即使对于需要辅助技术(屏幕阅读器等)的用户所使用的启用 JavaScript 的浏览器也会造成混淆。它使标记完全没有语义。不过既然你说的很清楚,这就是你想做的……
回答by Ibu
<a id='anchor' href="javascript to submit the form" ></a>
now you can use jquery to add an event handler
现在您可以使用 jquery 添加事件处理程序
$('#anchor').click(function (e) {
// do some work
// prevent the default anchor behaviour
e.preventDefault();
})
now you can style your anchor as you wish and it will act as a regular button
现在您可以根据需要设置锚点样式,它将充当常规按钮
回答by dani24
And what about:
还有:
<form id="formOne">
...
<a href="..." onclick="formOne.submit()">link here</a>
</form>
回答by Headshota
you can use input of type image (it works as a submit button for a form) or in jquery:
您可以使用图像类型的输入(它用作表单的提交按钮)或在 jquery 中:
$("a").click(function(event){
event.preventDefault();
$('form').submit();
})