Javascript submit textbox on ENTER [duplicate]

℡╲_俬逩灬. 提交于 2019-11-29 01:33:00
document.getElementById("id_of_your_textbox").addEventListener("keydown", function(e) {
    if (!e) { var e = window.event; }
    e.preventDefault(); // sometimes useful

    // Enter is pressed
    if (e.keyCode == 13) { submitFunction(); }
}, false);
nachito
$('#textboxId').keydown(function (event) {
    var keypressed = event.keyCode || event.which;
    if (keypressed == 13) {
        $(this).closest('form').submit();
    }
});

If you don't have form, then replace $(this).closest('form').submit(); with whatever AJAX/submit logic you have.

moranjk

You will need to listen for the ENTER key press event. Take a look at Enter key press event in JavaScript for some examples on how it is done.

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