Trigger a button click with JavaScript on the Enter key in a text box

后端 未结 30 1999
难免孤独
难免孤独 2020-11-21 23:53

I have one text input and one button (see below). How can I use JavaScript to trigger the button\'s click event when the Enter key is pressed ins

30条回答
  •  無奈伤痛
    2020-11-22 00:53

    This is a solution for all the YUI lovers out there:

    Y.on('keydown', function() {
      if(event.keyCode == 13){
        Y.one("#id_of_button").simulate("click");
      }
    }, '#id_of_textbox');
    

    In this special case I did have better results using YUI for triggering DOM objects that have been injected with button functionality - but this is another story...

提交回复
热议问题