开发者

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.

  1. You can use a button with type submit and use CSS to style it as a div
  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜