捕获jquery按键输入并记录到控制台

时间:2011-12-29 21:52:12

标签: jquery keypress

我的简单jQuery按键脚本无效。 我想要做的就是捕获textarea(id = story)上的输入并记录按下到控制台的键。我希望记录到控制台的字符是按下的实际字符而不是键码。我在keyup和keypress事件之间有一些混淆,但我已经尝试了两种,我根据jQuery文档构建了下面的代码。

<script>
$('#story').keypress(function()
{


    var txtInput = this.value;


    console.log(txtInput);

});
</script>

1 个答案:

答案 0 :(得分:1)

试试这个

$(document).ready(function() {
  $('#story').keyup(function()
    {   

        var txtInput = $(this).val();
        txtInput = txtInput.charAt(txtInput.length-1);
        alert(txtInput);

    });
});
相关问题