当您按Enter时触发HTML按钮 [英] trigger a HTML button when you press Enter

查看:916
本文介绍了当您按Enter时触发HTML按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用tampermonkey在特定页面上使按钮键触发".不必每次都单击鼠标来单击称为继续"的按钮.

I am trying to make a button key "trigger" on a specific page using tampermonkey. Instead of using mouse click every time to click Button called "continue".

因此,当我单击键盘按钮"ENTER"时,应自动单击此悬停按钮,在我可以放置此HTML代码的同时,可以使用任何可用的代码. 谢谢.

So when I click keyboard button "ENTER" this hover button should be automatically clicked, theres any ready to use code while I can just put this HTML code. Thanks.

<button id="simplicityPayment-START" type="submit" autocomplete="off" class="nf-btn nf-btn-primary nf-btn-solid nf-btn-oversize" data-uia="action-submit-payment">START MEMBERSHIP</button>

推荐答案

使用JQUERY

您可以使用简单的jQuery实现它.
只需在主HTML页面中添加以下给出的cdn

USING JQUERY

You can use easy jQuery to achieve it.
Just add a cdn given below in your main HTML page

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>

然后打开脚本标签并复制粘贴此代码.

And just open a script tags and copy paste this code.

$(document).keypress(function(event){

var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
    $("#simplicityPayment-START").click();
}});


修改1:因为不拥有该网站,使用坦帕蒙奇.好的,因此还有另一种方法可以实现,那就是Pure JAVASCRIPT.它不需要CDN. 只需添加此JAVASCRIPT.

EDIT 1: As don't own the website & using tampermonkey. Okay so there is another way to achieve this, that is Pure JAVASCRIPT. it requires no CDN. just Add this JAVASCRIPT.

window.addEventListener('keyup', function (e) {
  if (e.keyCode === 13) {
     var button = document.getElementById("simplicityPayment-START");
     button.click();
  }
}, false);

这篇关于当您按Enter时触发HTML按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆