简体   繁体   English

在输入类型=“文本”中按Enter键后如何调用JavaScript函数?

[英]How to call a JavaScript function after pressing Enter key in Input type=“text”?

How do I call a function, get() , which will retrieve meaning of a word typed in a text box after the user presses the Enter key? 我如何调用函数get() ,该函数将在用户按下Enter键后检索在文本框中键入的单词的含义?

I am adding my code below, nothing worked till now; 我在下面添加我的代码,到目前为止没有任何效果; please please add your corrections to the code, I know the code is too poor. 请把您的更正添加到代码中,我知道代码太差了。

<script type="text/javascript" src="jquery.js"> </script>


<script type="text/javascript">

function get() {
$('#meaning').hide();
$.post('data.php', { name: form.wordfield.value }, 
function(output)   {

$('#meaning').html(output).fadeIn(1000);
});
}



</script>



</head>
<body>

<div id="container" style="width:100%">
<div id="header">

<ul>

<!--  <li><a href="#">Home</a> </li>-->
</ul>


</div>
<div id="logo" style="width: 10%; float:left; position:fixed; top:0px;                 z-index:-1;">         <img src="logo.gif"></img></div>
<p>
<div id="searchform">
<form name="form" >

<input type="text" name="wordfield" onKeyup="get();"><input type="button"    value="Translate" onClick="get();"> </form>
</div>
<div id="meaningbox">
</div>
<div id="meaning">

</div>
</p>
<div id="ad" style="width: 10%;float:right;"><!-- add section --></div>
<div id="footer">footer div goes here </div>
</div>
</body>
</html>

You have to use the key code on the event object. 您必须在事件对象上使用键代码。 For enter, it is 13, so you check for 13. 输入是13,因此请检查13。

<input type="text" id="MyInput" />

document.getElementById("MyInput").addEventListener( "keydown", function( e ) {
    var keyCode = e.keyCode || e.which;
    if ( keyCode === 13 ) {
       // enter pressed
       get();
    }
}, false);

Well, you can bind an event to the keydown or keypress event, and test what key was pressed. 好了,您可以将事件绑定到keydownkeypress事件,并测试按下了哪个键。

But it's not usually a good idea. 但这通常不是一个好主意。 A key event may fire for other reasons, such as the user picking an entry from a keyboard IME. 按键事件可能由于其他原因而触发,例如用户从键盘IME中选择一个条目。

What you are really trying to do is emulate the behaviour of the Enter key submitting a form. 您真正想做的是模仿Enter键提交表单的行为。 So do it by actually creating a form, putting the input in it, and picking up the submit event on the form. 为此,请实际创建一个表单,将输入内容放入其中,然后在表单上获取submit事件。 You can then be sure you're only getting Enter presses that were intended as a submission, and not have to worry about other Enter presses (or shift-Enter or anything like that), and any device that can fill in forms but doesn't have a normal Enter button will still be able to use the page. 然后,您可以确保仅获得打算作为提交的Enter印刷机,而不必担心其他Enter印刷机(或shift-Enter或类似的东西),以及可以填写表格但没有填写的任何设备如果没有正常的输入按钮,仍然可以使用该页面。

<form id="lookup-form">
    <input id="lookup-field"/>
</form>

document.getElementById('lookup-form').onsubmit= function() {
    get(document.getElementById('lookup-field').value);
    return false;
};

(If you're feeling properly conscientious, you can make it a rel form with an action pointing at a server-side script that does the lookup, so that the functionality works for user-agents without JavaScript.) (如果您有适当的责任心,可以将其指定为相关形式,并通过action指向执行查找的服务器端脚本进行操作,以便该功能适用​​于不使用JavaScript的用户代理。)

Here's what worked for me: 这对我有用:

$("#inputIDHERE").bind('keypress', function(e) {
    if(e.which == 13) {
        alert('You pressed enter!');
        get();
    }
});

I also added <form></form> around my input tag, though that may not be necessary. 我也在输入标签周围添加了<form> </ form>,尽管可能没有必要。

Or you use this. 或者你用这个。 i think this is the best way because debugging with the console is way easier. 我认为这是最好的方法,因为使用控制台进行调试更容易。

<input id="lookup-field" onchange="name of the function"/>

Basically: 基本上:

  1. Listen for a keydown , keyup or keypress event 侦听keydownkeyupkeypress事件
  2. The event object will include the keycode, test that it is the enter key 事件对象将包含键码,测试其是否为Enter键
  3. Call the function 调用函数

Due to differences in how browsers handle event binding and keycodes, you are almost certainly best off using a library for this. 由于浏览器处理事件绑定和键码的方式不同,因此几乎可以肯定,最好使用库来实现这一点。 For example, with YUI 3 and its key-event module: 例如,使用YUI 3及其键事件模块:

Y.one('#id_of_my_input').on('key', get, 'press:enter');

Assuming you're using the jQuery JavaScript framework , the following code should work: 假设您使用的是jQuery JavaScript框架 ,则以下代码应正常工作:

$('#my_text_box').keypress(function(e) {
    if (e.which == 13) {
        get();
    }
});

13 is the key code for Enter. 13是Enter键的密码。

Ideally, the code should be a part of your document.ready setup. 理想情况下,代码应成为document.ready设置的一部分。

You are looking for jQuery's change event. 您正在寻找jQuery的change事件。

$('#TextBox').change(function(e) {
    doSomethingWithString( this.val() );
})

The suggestions regarding keydown and finding the enter key are also valid. 有关按下按键和找到Enter键的建议也有效。

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

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