监听键盘回车键(扫描枪为例)

编程入门 行业动态 更新时间:2024-10-10 12:17:47

监听键盘<a href=https://www.elefans.com/category/jswz/34/1666974.html style=回车键(扫描枪为例)"/>

监听键盘回车键(扫描枪为例)

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

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("\n"); 
var index;
if (i == -1) {
index = text.length;
} else {
index = i + text.length - 2;
} 
text = text.substring(0, index) + ",\n"; 
document.getElementById("TextBox1").innerText = text;
document.getElementById("Label2").innerText = text;
var last = text.substring(0,text.length-2).lastIndexOf("\n");
var nowstring = text.substring(last-1, text.length - 2);
document.getElementById("Label1").innerText = nowstring;
}
op.focus = true;
}
</script>

 

前台:

<div style="width: 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('\n');
//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>

 



转载于:.html

更多推荐

监听键盘回车键(扫描枪为例)

本文发布于:2024-02-24 18:34:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1696285.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回车键   为例   键盘   扫描枪

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!