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>



  • 相关阅读:
    C#简单操作XML文件的增、删、改、查
    一个感觉还算可以的验证码生成程序
    安装aclocal1报错问题
    php中soap 的使用实例无需手写WSDL文件,提供自动生成WSDL文件类
    ofstream和ifstream详细用法[转]
    [原]C++ Soap客户端实例
    PHP中文件读、写、删的操作
    C++ Boost Thread 编程指南
    (转)虚函数和纯虚函数区别
    strcpy和memcpy的区别
  • 原文地址:https://www.cnblogs.com/vakeynb/p/4938799.html
Copyright © 2011-2022 走看看