2015-11-02 63 views
-1

我想在輸入元素上激活keyup事件,問題是一旦我按下某個鍵就沒有觸發,我必須做另一個鍵盤事件來激活它,而不是正常!jQuery鍵盤功能不能正常工作

這裏是我的代碼:

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

我想 「的」 這個樣子,也沒有工作使用:

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

任何建議?

+0

它對我來說工作正常:http://jsfiddle.net/8ukL69r3/當然,事件中的邏輯不起作用,因爲我缺少代碼的其他部分,但事件會按照它應該發生的那樣觸發。 – Anders

+0

我看到它通常單獨工作,但對於我的代碼段它不是! – CairoCoder

+0

堆棧溢出不是一個調試服務。你不能只轉儲你的所有代碼。請閱讀[MCVE]。你需要提供一個*最小*的例子。 – Anders

回答

0

我發現這個問題,我不得不重新計算 「成本」 修改它,而不是之前,像這樣經過:

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

請嘗試以下

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

喜歡的東西this

+0

同樣的問題,我必須按另一個鍵! – CairoCoder