How can I run a javascript function when input changes

試著忘記壹切 提交于 2021-02-14 18:48:09

问题


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)"/>

回答1:


You need to use the "onblur" event. It fires when a control loses its focus, which seems to be exactly what you need.




回答2:


Call the function on the change event of text field:

<input name="username" onChange="check_user(this.value);" type="text"/>



回答3:


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.




回答4:


I think you are looking for check the duplication of user. you have to write server side code for that.



来源:https://stackoverflow.com/questions/4886114/how-can-i-run-a-javascript-function-when-input-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!