Javascript 输入更改时如何运行javascript函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4886114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I run a javascript function when input changes
提问by mamrezo
When a user creates a new account on my website I want to check if the name already exists. I want this check to run when they leave the username input box and go to enter a password.
当用户在我的网站上创建新帐户时,我想检查该名称是否已存在。我希望在他们离开用户名输入框并输入密码时运行此检查。
I tried :
我试过 :
<input type="text" onkeyup="check_user(this.value)"/>
采纳答案by Please treat your mods well.
You need to use the "onblur" event. It fires when a control loses its focus, which seems to be exactly what you need.
您需要使用“onblur”事件。它在控件失去焦点时触发,这似乎正是您所需要的。
回答by Awais Qarni
Call the function on the change
event of text field:
在change
文本字段的事件上调用函数:
<input name="username" onChange="check_user(this.value);" type="text"/>
回答by Ben
Because you used onkeyup
in your code example, it sounds like you are looking to call a function on every keystroke. If that is the case, then instead of onchange
- which fires when the input
loses focus - you should use oninput
which will fire anytime there is a change inside of input.
因为您onkeyup
在代码示例中使用过,所以听起来您希望在每次击键时调用一个函数。如果是这种情况,那么代替onchange
- 在input
失去焦点时触发- 您应该使用oninput
which 将在输入内部发生变化时触发。
回答by enthusiastic
I think you are looking for check the duplication of user. you have to write server side code for that.
我认为您正在寻找检查用户的重复。您必须为此编写服务器端代码。