Using a hotkey to submit an HTML form?

被刻印的时光 ゝ 提交于 2019-12-07 02:10:30

问题


I have a very simple HTML form, only containing a submit button and a textarea. Is there an easy way to use a hotkey to submit this form, as an alternative to pressing the button?

Thanks in advance :-)


回答1:


Submit buttons can have accesskeys, but there are drawbacks. Users would probably be better off just hitting the tab key to focus the submit button, and then pressing enter.




回答2:


you can use the "keyDown", "keyPress" events if you use a JS library (JQUERY or MOOTOOLS).. you can program your own Ctrl+S to submit.

On JQuery see this http://api.jquery.com/keypress/

On Mootools see this http://mootorial.com/wiki/mootorial/03-native/05-event




回答3:


Here is the jQuery method for adding a keypress listener to your button (by specifying the ID)... you can also do it on your page by changing "YourID" to "document" (without quotes, since document is a page element. I know keypress has been mentioned, figured it would be easier for others to have code here, and not have to move elsewhere.

$("#YourID").keypress(function (e) {
   var code = (e.keyCode ? e.keyCode : e.which); // grabs keycode pressed
   if (code == 13) {  // Code 13 is enter
        searchUPC();
   }
});


来源:https://stackoverflow.com/questions/3618280/using-a-hotkey-to-submit-an-html-form

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