doudou3213 2014-06-17 00:09
浏览 91

没有textarea的JQuery按键

I've been looking around for a while, and can't seem to find this answer. I am new to JQuery, and am more familiar with PHP (though am fully aware that the act is not possible in PHP, thus searching for the answer in JQuery)

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script>

$(document).ready(function(){

  $("#keyboard").keyup(function(e){
    var key = e.keyCode ? e.keyCode : e.which;

    if (key == 87) {

        alert("keyup event occured! The Ascii value of pressed key is: " + event.keyCode);
    }

  });

});

</script>

For starters, this is what I have as the key press. For now it's just an example, that if the 'w' key is pressed then released, it tells me basically, it's pressed.

My form looks like this:

<form action="URL">
    <input type="text" id="keyboard" />
</form>

In every instance I've found, it seems that you need to have a text area or something similar. Is the text area somehow required? I am looking to have the text area removed and just have the page respond to my clicking of the W button, anywhere.

On a side note, how would I get this to submit to php if that's possible?

  • 写回答

2条回答 默认 最新

  • dpgjci27392 2014-06-17 00:11
    关注

    use $(document).keyup(function(e){ ...

    that way you catch the key-events globally


    On your sidenote...

    • the URL you supply in the action-tag, that's the script that will be executed.
    • In that (php)script you can get the variables from the inputs you send along, with POST[].

    JS:

    <form action="path/to/script.php">
        <input type="text" id="keyboard" name="keyboard" />
    </form>
    

    PHP (path/to/script.php):

    $variable = $_POST['keyboard']; //this uses the name-tags from the inputs in the form
    

    Although you might want to look into Ajax-requests ($.post and $.get in jQuery), in a lot of cases those have advantages over the traditional form-based php calls..

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用