It is required to enter the password and press Enter to trigger a background event. It seems to be a very simple requirement, but it encounters many difficulties.
There is mainly a password input text box in HTML content and a button to trigger background events.
1. After the TextBox text box gets focus, press Enter directly, and the page will be refreshed.
After careful study of the code, it turns out that when there is only one text box control in the page, the page will refresh when pressing Enter.
How to handle it: Add a hidden TextBox control to the page.
The specific principle is unknown!
2. Use Jquery to implement the method:
$(document).ready(function(){ $("#tbPassword").focus(); $('#bPassword').keydown(function(e){ if(e.keyCode == 13){ $("#ctl00_ContentBody_btnAccept_linkButton")[0].click(); } }); });3. For various reasons, I cannot use Jquery implementation method, so I have to switch to JS. Its compatibility is the most troublesome problem.
document.getElementByIdx_x_x("tbPassword").onkeypress = function(event){ var keynum; if(window.event) // IE { keynum = window.event.keyCode; } else if(event.which) // Netscape/Firefox/Opera { keynum = event.which; } if (keynum == 13) document.getElementByIdx_x_x('ctl00_ContentBody_btnAccept_linkButton').click(); }illustrate:
Read the keyboard keys under IE:
keynum = event.keyCode; // letter d,keynum=100 keychar = String.fromCharCode(keynum); // Convert keynum to character d
Read keyboard keys in FireFox:
keynum = event.which; // letter d,keynum=100 keychar = String.fromCharCode(keynum); // Convert keynum to character d
For example, in IE, there are only keyCode attributes, while FireFox has which and charCode attributes, and Opera has keyCode and which attributes.
Therefore, this compatibility issue has been addressed in Jquery.
The above is all about this article, I hope it will be helpful to everyone's learning.