Html 使用 div 作为提交按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7463246/
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
Use a div as a submit button
提问by Syl
I want to transform a div composed of image + text (for translation) in a submit button. Is it possible?
我想在提交按钮中转换由图像 + 文本(用于翻译)组成的 div。是否可以?
I managed to use the image as submit but the text is not a link and it gives a 'broken' feeling to the whole image + text
我设法使用图像作为提交,但文本不是链接,它给整个图像+文本一种“破碎”的感觉
回答by Will
<div onclick="document.myform.submit()"></div>
回答by Trevor
Jquery usage.
jQuery 用法。
$('div').click(function () {
$('form').submit();
});
回答by danishgoel
You can do it in two ways.
您可以通过两种方式做到这一点。
- You can use a button with type submit and use CSS to style it as a
div
- Add an onClick on the div, and submit the form on the click event.
- 您可以使用类型为 submit 的按钮并使用 CSS 将其设置为
div
- 在 div 上添加一个 onClick,并在点击事件上提交表单。
For method 2 you can either do it through jQuery or without, With plain js
对于方法 2,您可以通过 jQuery 或不使用纯 js 来完成
<script>
function submitOnClick(formName){
document.forms[formName].submit();
}
</script>
<form id="myForm" ...>
....
<div onclick="submitOnClick('myForm')>
...
</div>
...
</form>
Trevor's answer shows how to do it with jQuery.
In his example just replace the 'div'
and 'form'
with the ids of your div and form as this:
特雷弗的回答展示了如何使用 jQuery 做到这一点。
在他的示例中,只需将'div'
和替换'form'
为您的 div 和表单的 ID,如下所示:
If ids of div and form are myDiv
and myForm
specify them as '#myDiv'
and '#myForm'
, here #
is used to specify the following string is an id of an element.
如果 div 和 form 的 id 是myDiv
并且myForm
将它们指定为'#myDiv'
and '#myForm'
,则此处#
用于指定以下字符串是元素的 id。