在文本框中按 Enter 时如何触发 HTML 按钮? [英] How to trigger HTML button when you press Enter in textbox?

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

问题描述

所以我到目前为止的代码是:

它不是在

中,而是在

中.但是,当我在名为addLinks"的 textbox 中键入内容时,我希望用户能够按 Enter 并触发linkadd"button,然后该按钮将运行 JavaScript 函数.
我该怎么做?
谢谢

我确实找到了这段代码,但它似乎不起作用.

$("#addLinks").keyup(function(event){如果(事件.keyCode == 13){$("#linkadd").click();}});

解决方案

$(document).ready(function(){$('#TextBoxId').keypress(function(e){if(e.keyCode==13)$('#linkadd').click();});});

So the code that I have so far is:

<fieldset id="LinkList">
    <input type="text" id="addLinks" name="addLinks" value="http://">
    <input type="button" id="linkadd" name="linkadd" value="add">
</fieldset>

It is not in a <form> and is just as it is within a <div>. However when I type something into the textbox called "addLinks" I want the user to be able to press Enter and trigger the "linkadd" button which will then run a JavaScript function.
How can I do this?
Thanks

Edit: I did find this code, but it doesnt seem to work.

$("#addLinks").keyup(function(event){
    if(event.keyCode == 13){
        $("#linkadd").click();
    }
});

解决方案

$(document).ready(function(){
    $('#TextBoxId').keypress(function(e){
      if(e.keyCode==13)
      $('#linkadd').click();
    });
});

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

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