繁体   English   中英

文本框键入事件无法正常工作

[英]textbox keyup event not working properly

我正在使用以下代码根据其他文本框txtQuantity更新一个文本框值txtUnitPrice,两者都可以动态生成。

    //update unitprice based on quantity of product and tax applied
    $('input').live('keyup', function () {
        var inputId = $(this).attr('id');  //get textbox txtQuantity id
        var rowNum = parseInt(/txtQuantity(\d+)/.exec(inputId)[1], 10); //get row id
        var productValue = $("#ddlProduct" + rowNum).val();
        var taxValue = $("#ddlTax" + rowNum).val();
        var unitPriceValue = $("#txtUnitPrice" + rowNum).val();
        var quantityValue = $("#txtQuantity" + rowNum).val();


        if ((quantityValue != " ") && (quantityValue > 0)) {
            $("#txtUnitPrice" + rowNum).val(unitPriceValue * quantityValue);
        }
        else {
            $("#txtUnitPrice" + rowNum).val(unitPriceValue);
        }

    }); 

现在,上面的代码中出现的问题是,当我甚至在文本框txtQuantity中向前或向后移动光标时,txtUnitPrice的值都在更改,因为触发了该事件“ keyup”,因此整个代码都将使用现有的txtunitprice值执行并相乘再次使用现有的txtquantity,这是我不想要的。 谢谢

只需添加一个检查即可忽略光标移动,如下所示

$('input').live('keyup', function (e) {
    if([37, 38, 39, 40].indexOf(e.which)){
        return false;//do nothing because the cursor keys have been pressed
    }
    //the rest of your code
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM