javascript - 带有 JQuery 事件的条码阅读器

标签 javascript jquery asp.net

我正在使用 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/

相关文章:

jquery - 为什么我的导航在使用 Fullpage.js 时不是垂直的?

c# - 如何向 Array.IndexOf 添加不区分大小写的选项

c# - MVC 3 Multi-Tenancy 和 View 编译缓存问题

javascript - 使用 AES 的 CryptoJS 库时解密输出不正确

Javascript/jQuery : Convert key combo to string?

javascript - 在nodejs中将缓冲区转换为视频时出错

javascript - 将参数从 JSP 传递到 Struts 2 中的操作类

java - Spring MVC - 将 JSON 对象从 Ajax post 请求映射到 Java 对象

jquery - 如何获取动态字段值 - jQuery

asp.net - 有时无法从 Azure 网站访问 Azure SQL 数据库