Cross-browser handling the “Enter” key pressing using Javascript

后端 未结 1 1388
太阳男子
太阳男子 2021-01-24 09:46

I have got the following sample, that easily detects the \"Enter\" key pressing and handles it correctly. Here it is:





        
1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-24 10:11

    The problem is solved using the following code for handling pressing the "Enter" key:

    $("#search-button").on('click', function () {
        var theUrl = "/search.aspx?search=" + $('#search-input').val();
        window.location = theUrl;
    });
    $('#search-input').on('keyup', function (e) {
        if (e.which == 13)
            $("#search-button").trigger('click');
    });
    

    This code is built into the $(document).ready function.

    0 讨论(0)
提交回复
热议问题