简体   繁体   中英

how to trigger the button of the website when i press the enter key?

there is a button in a website ,i want to trigger the button when i press enter key, i write the javascript for the same but my script is works suddenly for 2 or 3 second and then again stop working i wont be getting any solution ,please help me for this. i am using greasemonkey for this. my javascript is :

 // ==UserScript== // @name Enter 2 // @namespace www.irctc.co.in/eticketing/abubakar.js // @description press enter to go payment area // @include https://www.irctc.co.in/eticketing/* // @grant none // ==/UserScript== document.addEventListener("keydown", keyDownTextField, false); function keyDownTextField(e) { var keyCode = e.keyCode; if(keyCode==13) { document.getElementById("validate").click(); } } 

sorce code of button is:

 <input id="validate" type="button" value=" Next "> 

Works fine for me: http://jsfiddle.net/theagitator/vtn4c0nj/

<input id="validate" type="button" value="Next" onclick='alert("yo")'>

Notice, that you have to first click somewhere in the result window to make it active before pressing the enter.

This is not an answer, but I don't have enough reputation to comment on your question.

It could be that another element has focus, and is grabbing the key press. This can happen if there are multiple elements on a page that can be triggered by the Enter key.

As an alternative, if you set your input type to submit , the Return/Enter key will trigger that button without any JavaScript required, and it may solve your issue. If you only have one such input, you don't need to place it within a form element.

<input type="submit" id="validate" value="Next" onclick="doSomething();" />

Please try this code for captcha answer box :

<input id="target"
       class="text captcha-answer"
       type="text"
       autocomplete="off"
       maxlength="8"
       style="width:80px"
       name="j_captcha">

It should be work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM