Submit form with Enter key without submit button? [duplicate]

落花浮王杯 提交于 2019-11-26 05:16:55

问题


Possible Duplicate:
HTML: Submitting a form by pressing enter without a submit button

How can I submit a form with just the Enter key on a text input field, without having to add a submit button?

I remember this worked in the past, but I tested it now and the form doesn\'t get submitted unless there\'s a submit-type input field inside it.


回答1:


$("input").keypress(function(event) {
    if (event.which == 13) {
        event.preventDefault();
        $("form").submit();
    }
});



回答2:


Change #form to your form's ID

$('#form input').keydown(function(e) {
    if (e.keyCode == 13) {
        $('#form').submit();
    }
});

Or alternatively

$('input').keydown(function(e) {
    if (e.keyCode == 13) {
        $(this).closest('form').submit();
    }
});



回答3:


Jay Gilford's answer will work, but I think really the easiest way is to just slap a display: none; on a submit button in the form.




回答4:


@JayGuilford's answer is a good one, but if you don't want a JS dependency, you could use a submit element and simply hide it using display: none;.



来源:https://stackoverflow.com/questions/8981637/submit-form-with-enter-key-without-submit-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!