【问题标题】:jQuery onkeyup method not definedjQuery onkeyup 方法未定义
【发布时间】:2012-06-18 15:31:06
【问题描述】:

onkeyup 方法应该没有定义,但是,该方法是我的 ide 自动推荐给我的。当我在 chrome 开发工具中查看错误时,我收到错误 Uncaught TypeError: Object [object Object] has no method 'onkeyup'。我正在使用最新版本的 jQuery。这是我的代码:

    <!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("button").click(function(){
                var str = document.getElementById('txt1').value;
                $.post("addName.php", {q: str});
            });
            $("input").onkeyup(function(){
                var str = document.getElementById('txt1').value;
                $.post("gethint.php", {q: str});
            });
        });
    </script>
</head>
<body>

<h3>Start typing a name in the input field below:</h3>
<form action="">
    First name: <input type="text" id="txt1"/>
</form>
<p>Suggestions: <span id="txtHint"></span></p>
<button> Add Name</button>

</body>
</html>

【问题讨论】:

    标签: jquery key-events


    【解决方案1】:
    $("input").on('keyup', function(){
        var str = document.getElementById('txt1').value;
        $.post("gethint.php", {q: str});
    });
    

    $("input").keyup( function(){
        var str = document.getElementById('txt1').value;
        $.post("gethint.php", {q: str});
    });
    

    jQuery 没有方法名onkeyup

    详细了解 .keyup().on()

    【讨论】:

    • 谢谢。这解决了问题。我正确使用 $.post ,对吗?因为现在事件正在被触发,我认为脚本不会一直在运行:/ 我知道这一点,因为我在文件顶部的回声永远不会输出。
    • 只是想知道为什么要使用: var str = document.getElementById('txt1').value;而不是: var str = $('#txt1').val();由于您使用的是 Jquery
    【解决方案2】:

    方法是:

    $("input").keyup(function() {
    })
    

    【讨论】:

      【解决方案3】:

      使用 keyup 代替 onkeyup

       $("input").keyup(function(){
                      var str = document.getElementById('txt1').value;
                      $.post("gethint.php", {q: str});
                  });
      

      【讨论】:

        【解决方案4】:

        方法onkeyup不存在,错误信息明确写着"onkeyup method not defined"。应该只是keyup

        $("input").keyup(function() {
            //Your code
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-28
          • 2019-11-03
          • 1970-01-01
          相关资源
          最近更新 更多