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

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

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>

 



转载于:https://www.cnblogs.com/vakeynb/p/4938799.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值