[英]Call javascript function on enter of a text input, without using jquery
Is it possible to call a javascript function on enter of a text input, without using jquery?是否可以在不使用 jquery 的情况下在输入文本时调用 javascript 函数?
<input name = 'text' type = 'text' onEnter('callJavascriptFunction')>
^---it would be preferable for the onEnter to be inside the element like above... ^---最好让 onEnter 像上面一样位于元素内部......
Sure is:当然是:
<input name="text" type="text" onkeyup="callJavascriptFunction();">
You can also do it without the inline javascript:您也可以在没有内联 javascript 的情况下执行此操作:
<input id="myTextBox" name="text" type="text">
Then in your js file:然后在你的 js 文件中:
var myTextBox = document.getElementById('myTextBox');
myTextBox.addEventListener('keyup', function(){
//do some stuff
});
Edit: If you're looking for enter press:编辑:如果您正在寻找输入按:
<input id="myTextBox" name="text" type="text">
Then in your js file:然后在你的 js 文件中:
var myTextBox = document.getElementById('myTextBox');
myTextBox.addEventListener('keypress', function(){
if(e.keyCode == 13){//keyCode for enter
//do some stuff
}
});
Use the event onchange .使用事件onchange 。 You can do this on HTML:您可以在 HTML 上执行此操作:
<input onchange="fncDoThings()">
And your JS file can be like this:你的 JS 文件可以是这样的:
function fncDoThings() {
console.log('hello'); //just an example of things to do inside the function
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.