zoukankan      html  css  js  c++  java
  • 监听键盘回车键(扫描枪为例)

    监听键盘回车键,以扫描枪为例

    1)IE可用: 

    <script type="text/javascript" language="javascript" charset="UTF-8">
    function enter(op) {
    
    if (event.keyCode != 13) {
    return
    }
    if (event.keyCode == 13 || op.focus) {
    
    var text = document.getElementById("TextBox1").innerText;
    var i = text.lastIndexOf("
    "); 
    var index;
    if (i == -1) {
    index = text.length;
    } else {
    index = i + text.length - 2;
    } 
    text = text.substring(0, index) + ",
    "; 
    document.getElementById("TextBox1").innerText = text;
    document.getElementById("Label2").innerText = text;
    var last = text.substring(0,text.length-2).lastIndexOf("
    ");
    var nowstring = text.substring(last-1, text.length - 2);
    document.getElementById("Label1").innerText = nowstring;
    }
    op.focus = true;
    }
    </script>
    
     

    前台:

    <div style=" 208px; height: 400px; margin: 0 auto; background-color:Silver">
    条码号:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server" onkeyup="enter(this);" Width="208px" TextMode="MultiLine"
    Height="270px"></asp:TextBox>
    <%--<input onclick="enter(this)" onkeypress="enter(this)" value="" style="height:50px;" size="24px" >--%>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

    2)IE和谷歌可用

    <script type="text/javascript">
    //注册键盘事件
    document.onkeydown = function (e) {
    //捕捉回车事件
    var ev = (typeof event != 'undefined') ? window.event : e;
    if (ev.keyCode == 13 && document.activeElement.id == "TextBox1") {
    //获取到所有内容
    var _value = document.activeElement.value;
    var index = _value.indexOf('
    ');
    //alert(index);
    document.getElementById("TextBox1").value = _value + ",";
    document.getElementById("Label1").innerText = document.getElementById("TextBox1").value;
    }}
    </script>

     前台:  

    <div>
    <asp:TextBox ID="TextBox1" runat="server" Width="208px" TextMode="MultiLine" Height="270px"></asp:TextBox><br />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>



  • 相关阅读:
    大道至简读后感
    机器学习十讲(一)
    第一个TensorFlow的简单例子
    初识深度学习
    如何使用本地的Navicat连接服务器中的Mysql
    阿里云ECS-安装Tomcat
    阿里云ECS-CentOS 8上安装MySQL 8.0
    阿里云ECS--CentOS8安装jdk1.8
    进度报告十(重大技术需求)
    进度报告九 (重大技术需求调研)
  • 原文地址:https://www.cnblogs.com/vakeynb/p/4938799.html
Copyright © 2011-2022 走看看