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

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

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>

 



posted @ 2015-11-05 11:03  疯狂的多多  阅读(3504)  评论(0编辑  收藏  举报