Maison  >  Questions et réponses  >  le corps du texte

javascript - 谁用过input事件?如何在inpu的事件对象中获取自己输入的字符?

谁用过input事件?如何在inpu的事件对象中获取自己输入的字符?

回答的都不行,我需要判断当前按下的值,你们这样的我无法检测到backspace被按下

微信不支持keyup,keydown,keypress中的keyCode,所以,keycode这种思路无效

迷茫迷茫2726 Il y a quelques jours362

répondre à tous(2)je répondrai

  • 天蓬老师

    天蓬老师2017-04-11 11:40:17

    event.target.value

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <input id='test' type='text'>
    <script>
        document.getElementById('test').addEventListener('input',function(e){
            console.log(e.target.value);
        })
    </script>
    </body>
    </html>

    -----分割线----
    是回答得不行,还是你自己没有讲清楚?
    backspace的keycode是8

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <input id='test' type='text'>
    <script>
        document.getElementById('test').addEventListener('keyup',function(e){
            if(e.keyCode==8){
                console.log('按了backspace键');
            }
        })
    </script>
    </body>
    </html>

    répondre
    0
  • 迷茫

    迷茫2017-04-11 11:40:17

    html

    <input type="text" id="test">

    javascript

    // jquery
    $('#test').on('keyup', function(event) {
      console.log(event.key)
    })
    
    // 原生
    var input = document.getElementById("test")
    input.addEventListener('keyup', function(event) {
      console.log(event.key)
    })

    répondre
    0
  • Annulerrépondre