Use a div as a submit button
I want to transform a 开发者_JAVA技巧div composed of image + text (for translation) in a submit button. Is it possible?
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
<div onclick="document.myform.submit()"></div>
Jquery usage.
$('div').click(function () {
$('form').submit();
});
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.
For method 2 you can either do it through jQuery or without, With plain 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:
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.
精彩评论