I'm writing some unit test in my web apps and I want to automatically trigger an Enter key pressed event from my page after for example a download to text file button was clicked. How can i do this in jQuery or Javascript?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
If you're talking about automatically entering an Enter key to trigger a
submit
event, you can use this instead:To trigger this event after a button has been pressed, try this:
Create a variable that tells us if the user has clicked your button.
Add an event listener to your button, so that when the user clicks on it, the
clicked
variable will becometrue
.Add a
keypress
event listener:Inside that event listener, check if the user clicked previously
If (s)he did, check the pressed key. Enter's key code is 13.
If the pressed key was enter, use
Otherwise, once the user clicked your button and pressed enter, it wouldn't be necessary to click the button again.
After that run your code. For example, this will call function
f
after 2 seconds.Live demo
If I get your question right, this is what you are looking for