Setting focus to a button next to a text box

末鹿安然 提交于 2020-01-06 08:41:13

问题


I have a question that is very similar to this one: Setting focus to a button from from text box?

On my page, there is a text box, with a button next to it. It is an ASP.NET page, but the button is just a standard input tag, and when clicked, there is a client side function called.

I want it so that when you hit enter, that button is clicked. I have managed to set focus initially using $("#mybutton").focus(), but when you click into the text box, the button loses the focus.

Is there any way to achieve what I want here?


回答1:


I'm not quite sure if this is what you're asking, but this will watch for a keypress on the enter key and click the button when it happens.

$('#theinput').keypress(function(event) {
  if (event.keyCode == '13') {
    $('#mybutton').click();
    event.preventDefault();
  }
});


来源:https://stackoverflow.com/questions/3839342/setting-focus-to-a-button-next-to-a-text-box

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