我正在使用 asp.net 客户端页面,其中有一个文本框
<asp:TextBox class="form-control " autocomplete="off" ID="txtScan" runat="server"></asp:TextBox>
使用 jquery 我试图在条形码阅读器读取值时获取值
$(document).ready(function () {
$('#<%=txtScan.ClientID%>').on('change', function() {
console.log( $('#<%=txtScan.ClientID%>').val())
});
});
我希望在条形码阅读器完全读取完输入值后获取输入值。因此,我想使用 ajax 执行一些服务器端操作。这应该在不失去输入焦点的情况下完成。
当我使用 aspx 文本框 OnTextChanged 事件时,我实现了这一点。但现在我想用 jquery/javascript 来做这件事
最佳答案
我建议添加一个计时器,并在执行任何操作之前给用户一些时间进行交互,而不是直接触发任何东西。
例如你可以这样做:
var pTimerSelCntr = null;
$(document).ready(function () {
$('#<%=txtScan.ClientID%>').on('change', function() {
// clear the time out, so the function is not fired
if (pTimerSelCntr)
clearTimeout(pTimerSelCntr);
// start new time out to fire the function
// and give some time to the user to interact again
// before you do any action
pTimerSelCntr = setTimeout(function() {
console.log( $('#<%=txtScan.ClientID%>').val())
}, 800);
});
});
The issue i am facing is that this change event gets fired for each character entry and continuously
这是不正常的,也许其他东西也在与您的编辑器交互,但是使用上面的代码您可能会避免冲突和时间竞赛问题,在这些情况下不止一次尝试在编辑器上思考。
关于javascript - 带有 JQuery 事件的条码阅读器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59465551/