简体   繁体   English

jQuery keyup 功能无法正常工作

[英]jQuery keyup function is not working properly

I am trying to activate keyup event on an input element, the problem is it didn't trigger once I press a key, I have to do another key event to activate it, which is not normal!我试图在输入元素上激活keyup事件,问题是我按下一个键后它没有触发,我必须执行另一个键事件才能激活它,这是不正常的!

Here's my code:这是我的代码:

$(".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();
});

I tried to use on like this, but also not working:我试图用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();
});

Calculate shipping function:计算运费函数:

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));
}

Any advice?有什么建议吗?

Try the following尝试以下

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

Something like this这样的东西

I found the problem, I have to recalculate the ".cost" after modifying it, not before, like 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());
});

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

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