jQuery keyup 功能无法正常工作

jQuery keyup function is not working properly

我正在尝试激活输入元素上的 keyup 事件,问题是我按下某个键后它没有触发,我必须执行另一个按键事件来激活它,这是不正常的!

这是我的代码:

$(".conv").keyup(function(){
    var itemId = new Array();
    
    $(".itemId").each(function() {
        itemId.push($(this).text());
    });

    if (itemId.length == 0 || isEmpty($(this).val())) {
        $(this).val('0.00');
        return false;
    }

    if (!$.isNumeric($(this).val())) {
        alert("Only numeric values are allowed in '" + $(this).parent().parent().find('th').text() + "' field.");
        return false;
    }

    calcShipping();
});

我试过这样使用 on,但还是不行:

$('body').on("keyup", ".plus", function(){
    var itemId = new Array();

    $(".itemId").each(function() {
        itemId.push($(this).text());
    });

    if (itemId.length == 0) {
        $(this).val('0.00');
        return false;
    }

    calcShipping();
});

计算运费函数:

var calcShipping = function() {

    if (isEmpty($(".totalCost2").text())) {
        $(".totalCost2").text(parseInt($(".totalCost").text()));
    }

    var plus      = 0;
    var conv      = parseInt($(".conv").val());
    var tCost     = parseInt($(".totalCost2").text());
    var tQty      = 0;
    var tFinal    = 0;
    var tLast     = 0;
    var qty       = 0;
    var totalCost = 0;
    var cost      = new Array();
    var qtyCost   = new Array();

    $("#txtItems2").attr('disabled','disabled');
    $("#btnReset2").attr('disabled','disabled');
    $("#uploadItems").attr('disabled','disabled');
    $("#startUpload").attr('disabled','disabled');

    $(".plus").each(function(){
        if (isEmpty($(this).val())) {
            $(this).val('0.00');
        }
        if (!$.isNumeric($(this).val())) {
            alert("Only numeric values are allowed in '" + $(this).parent().parent().find('th').text() + "' field.");
            return false;
        }
        plus += parseInt($(this).val());
    });

    $(".qty").each(function() {
        qtyCost.push(parseInt($(this).text()));
        tQty += parseInt($(this).text());
    });

    $(".cost").each(function() {
        cost.push($(this).text());
    });

    tFinal = plus + tCost;
    tLast  = tFinal/tQty;
    
    var qty = 0;

    $(".cost").each(function(){
      qty = parseInt($(this).parent().parent().find($(".qty")).text());
      $(this).text(tLast * qty * conv);
      qty = 0;
    });

    for (var i = 0; i < cost.length; i++)
    {
        totalCost += (cost[i] * qtyCost[i]);
    }

    $(".totalCost").text(totalCost.toFixed(2));
}

有什么建议吗?

尝试以下方法

$('.conv').on("input", function() {
    //do what you need to do here
});

类似于this

我发现问题了,我修改后要重新计算“.cost”,而不是之前,像这样:

$(".cost").each(function(){
      qty = parseInt($(this).parent().parent().find($(".qty")).text());
      var newCost = tLast * qty * conv;
      $(this).text(newCost);
      qty = 0;
});

$(".cost").each(function() {
    cost.push($(this).text());
});