开发者

Make an anchor acts as a submit button. Not just using click event

开发者 https://www.devze.com 2023-04-12 13:38 出处:网络
Something like this will works only when user clicks the anchor. But a form also is submitted when user push [enter] key inside the form. How could I have the same functionality using just an anchor?

Something like this will works only when user clicks the anchor. But a form also is submitted when user push [enter] key inside the form. How could I have the same functionality using just an anchor?

    $('a.submit').click(function() {
    $(this).closest('form').submit();
}); 

Thanks.-

EDIT (with solution):

This combination works (at least) in FF and chrome! either clicking the anchor with class submit or pressing [e开发者_JAVA技巧nter].

    $('#my_form a.submit').click(function(e) {
    e.preventDefault();
    $(this).closest('form').submit();
}); 

$('#my_form').keydown(function(e) {
    if (e.keyCode != 13)
        return;
    $(this).submit();
}); 

Thanks for your comments!


Need to prevent the click event from firing the anchor:

$('a.submit').click(function(event) {
    event.preventDefault();
    $(this).closest('form').submit();
}); 


After several test this is perfectly working:

$('#my_form a.submit').click(function(e) {
    e.preventDefault();
    $(this).closest('form').submit();
}); 

$('#my_form').keydown(function(e) {
    if (e.keyCode != 13) // Enter or Return keystroke
        return;
    $(this).submit();
});
0

精彩评论

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