如何在输入更改时运行javascript函数

时间:2022-12-08 03:35:07

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

4 个解决方案

#1


7  

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

您需要使用“onblur”事件。它会在控件失去焦点时触发,这似乎正是您所需要的。

#2


7  

Call the function on the change event of text field:

在文本字段的change事件上调用该函数:

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

#3


2  

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--当输入失去焦点时触发 - 你应该使用oninput,它会在输入内部发生变化时触发。

#4


1  

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

我认为你正在寻找检查用户的重复。你必须为此编写服务器端代码。

#1


7  

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

您需要使用“onblur”事件。它会在控件失去焦点时触发,这似乎正是您所需要的。

#2


7  

Call the function on the change event of text field:

在文本字段的change事件上调用该函数:

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

#3


2  

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--当输入失去焦点时触发 - 你应该使用oninput,它会在输入内部发生变化时触发。

#4


1  

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

我认为你正在寻找检查用户的重复。你必须为此编写服务器端代码。