简体   繁体   English

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

[英]How can I run a javascript function when input changes

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

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

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

Because you used onkeyup in your code example, it sounds like you are looking to call a function on every keystroke.因为您在代码示例中使用了onkeyup ,所以听起来您希望在每次击键时调用一个函数。 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.如果是这种情况,那么代替onchange - 当input失去焦点时触发 - 您应该使用oninput ,它会在输入内部发生变化时触发。

You need to use the "onblur" event.您需要使用“onblur”事件。 It fires when a control loses its focus, which seems to be exactly what you need.它在控件失去焦点时触发,这似乎正是您所需要的。

I think you are looking for check the duplication of user.我想你正在寻找检查用户的重复。 you have to write server side code for that.您必须为此编写服务器端代码。

If you want to fire when something specific is type into the window:如果您想在窗口中输入特定内容时触发:

phr = 'lol'
ltr = phr.split('')
text = ''

document.addEventListener('keyup',function(k){
    if (ltr.includes(k.key)) {
        text += k.key
        if (text == phr) {
            console.log('Typed!')
            text = ''
        }
    } else {
        text = ''
    }})

As a function your can use it like:作为一个函数,你可以像这样使用它:

function wtt(phrase,clb) {
    phr = phrase
    ltr = phr.split('')
    text = ''

    document.addEventListener('keyup',function(k){
        if (ltr.includes(k.key)) {
            text += k.key
            if (text == phr) {
                clb()
                text = ''
            }
        } else {
            text = ''
        }})
}

wtt('my phrase', function () {
    console.log('dostuff')
})

暂无
暂无

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

相关问题 当innerHtml随着Ajax更改时,我如何调用JavaScript函数运行? - How can i call a javascript function to run when innerHtml changes with Ajax? 输入更改后,如何使用javascript调用函数? - how can i call a function with javascript as soon as the input changes? 使用datepicker输入更改时运行javascript函数 - Run a javascript function when input changes using datepicker 输入更改值时如何在Javascript中调用函数? - How to call a function in Javascript when a input changes value? 当隐藏的输入框的文本由外部jquery更改时,如何触发函数? - How can I trigger a function when the text of a hidden input box changes by an external jquery? 如何通过接受用户输入和使用按钮来运行JavaScript函数? - How can I run a JavaScript function by taking user input & using a button? 如何通过输入字段和按钮作为 HTML 中的事件运行 if/else Javascript function? - How can I run an if/else Javascript function through and input field and a button as the event in HTML? 如何编辑此 function 以仅在输入字段完成时运行? - How can I edit this function to run ONLY when input fields are completed? 当我更改输入时,如何在 HTML 表单上自动显示 JavaScript function 的结果? - How can I automatically show the result of a JavaScript function on a HTML form, when I change the input? 输入焦点时运行功能的Javascript - Javascript to run a function when input focus
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM