开发者

Create a handler for myform.submit() which works from a button click

For some reason the submit event is different in these 2 cases. Please hel开发者_StackOverflowp understand why. Here is a form with a submit and a button. Click submit and alert fires, click button and the alert doesn't fire. What's different about the form element? How do I make both work the same? BTW: I am testing in Chrome. This needs to work for all browsers that support myform.submit();.

<FORM id="myform" action="saveform.asp" method="post">
<input type="hidden" name="my_val" value="" />
<INPUT id="button" value="Save Button" class="save-submit" type=button >
<INPUT id="button" value="Save Submit" class="save-submit-hidden" type=submit>
</FORM>
<script language=javascript>


        $("form").submit(function() {
            alert('submitting');
            return false;
        });


    $('.save-submit').click(function() {
        myform.submit();
    });
</script>


myform.submit();

should be

$(myform).submit();

and with that it works for me.

EDIT:

This seems to be a misfeature in jQuery: jQuery override form submit not working when submit called by javascript on a element


Just delete all of the javascript. Clicking an input of type=submit on a form causes it to submit, no javascript required. Unless you need some javascript to run every time the form is submitted (and no matter HOW it is submitted), in that case use your $("form").submit function and delete the $('.save-submit').click function.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜