简体   繁体   English

当我按下回车键时如何触发网站的按钮?

[英]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. 网站上有一个按钮,当我按Enter键时,我想触发该按钮,我用同样的方式编写javascript,但是我的脚本突然工作了2或3秒钟,然后再次停止工作,我将无法获得任何解决方案,请帮我。 i am using greasemonkey for this. 我为此使用滑脂。 my javascript is : 我的JavaScript是:

 // ==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/ 对我来说效果很好: 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. 请注意,必须先在结果窗口中的某个位置单击才能使其处于活动状态,然后再按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. 如果页面上有多个可由Enter键触发的元素,则会发生这种情况。

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. 或者,如果将输入类型设置为submit ,那么Return / Enter键将触发该按钮,而无需任何JavaScript,这可能会解决您的问题。 If you only have one such input, you don't need to place it within a form element. 如果只有一个这样的输入,则无需将其放置在form元素中。

<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. 应该是可以的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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