2015-02-11 45 views
0

我的腳本運行時遇到一些問題。我的腳本計算訂單的數量和每個訂單的重量。按下「新行」按鈕時,我的表單實際上是由javascript創建的。我試圖在3個值(物品數量,箱子中的物品數量,每個訂單的箱子數量)填入時自動給出訂單總數。另外,當給定單個物品的重量時,它會給出訂單的總重量。所有這些都發生在使用jquery的keyup上。我只需要在javaScript部分中提供幫助。我後來用AJAX處理了我的表單,一切正常,但是當我試圖實現這一點時它破裂了。 ($ totalitems) - > [object Object]和alert($ weightperpal) - > NaN。當我使用alert()來查看我的代碼分配給updateForm()函數的值是什麼時,有人能幫我解決這個問題嗎?我的繼承人代碼:jQuery從變量中取回NaN和[object Object]

HTML:

<button id="addLine" type="submit" class="btn red">Add Line</button> 

的JavaScript:

$("#addLine").click(function(e){ 
    e.preventDefault(); 
    lineNum++; 

    var cont = '<div class="control-group">'+ 
     '<label class="control-label">Total # of items</label>'+ 
     '<div class="controls">'+ 
     '<input type="text" placeholder="" class="m-wrap medium orderField" name="totalitems'+lineNum+'" id="totalitF" no="'+ lineNum +'"/>'+ 
     '<span class="help-inline" style="font-size:10px; font-style:italic;">Not mandatory</span>'+ 
     '</div>'+ 
     '</div>'+ 
     '<div class="control-group">'+ 
     '<label class="control-label">Items per box</label>'+ 
     '<div class="controls">'+ 
     '<input type="text" placeholder="0" class="m-wrap medium orderField" name="ipb'+lineNum+'" id="itemspbF" no="'+ lineNum +'"/>'+ 
     '<span class="help-inline" style="font-size:10px; font-style:italic;">#</span>'+ 
     '</div>'+ 
     '</div>'+ 
     '<div class="control-group">'+ 
     '<label class="control-label">Boxes per order</label>'+ 
     '<div class="controls">'+ 
     '<input type="text" placeholder="0" class="m-wrap medium orderField" name="bpp'+lineNum+'" id="boxesppF" no="'+ lineNum +'"/>'+ 
     '<span class="help-inline" style="font-size:10px; font-style:italic;">#</span>'+ 
     '</div>'+ 
     '</div>'+ 
     '<div class="control-group">'+ 
     '<label class="control-label">Weight per Item</label>'+ 
     '<div class="controls">'+ 
     '<input type="text" placeholder="0" class="m-wrap medium orderField" name="wpi'+lineNum+'" id="weightpiF" no="'+ lineNum +'"/>'+ 
     '<span class="help-inline" style="font-size:10px; font-style:italic;">#</span>'+ 
     '</div>'+ 
     '</div>'+ 
     '<div class="control-group">'+ 
     '<label class="control-label">Total number of Orders</label>'+ 
     '<div class="controls">'+ 
     '<input type="text" placeholder="" class="m-wrap medium orderField" name="pallets'+lineNum+'" id="palletsF" no="'+ lineNum +'"/>'+ 
     '<span class="help-inline" style="font-size:10px; font-style:italic;"># of pallets</span>'+ 
     '</div>'+ 
     '</div>'+ 
     '<div class="control-group">'+ 
     '<label class="control-label">Weight per order</label>'+ 
     '<div class="controls">'+ 
     '<input type="text" placeholder="" class="m-wrap medium orderField" name="wpp'+lineNum+'" id="weightppF" no="'+ lineNum +'"/>'+ 
     '<span class="help-inline" style="font-size:10px; font-style:italic;">Total number of boxes</span>'+ 
     '</div>'+ 
     '</div>'+ 
     '</div>'; 

    $(document).on('keyup','.weightpiF',function(){ 
     var valueField = $(this).attr('no'); 
     updateFormRoe(valueField); 
    }); 
    $(document).on('keyup','.weightppF',function(){ 
     var valueField = $(this).attr('no'); 
     updateFormRoe(valueField); 

    }); 
    $(document).on('keyup','.itemspbF',function(){ 
     var valueField = $(this).attr('no'); 
     updateFormRoe(valueField); 
    }); 
    $(document).on('keyup','.boxesppF',function(){ 
     var valueField = $(this).attr('no'); 
     updateFormRoe(valueField); 
    }); 

    function updateFormRoe(number){ 
     alert(number); 
     var $totalitems = $('#totalitF'+number); 
     alert($totalitems); 
     var $itemspb = $('#itemspbF'+number).val(); 

     var $boxespp = $('#boxesppF'+number).val(); 

     var $weightperitem = $('#weightpiF'+number).val(); 

     var $itemsinpallet = $itemspb * $boxespp; 

     var $totalpals = $totalitems/$itemsinpallet; 

     var $weightperpal = ($itemsinpallet) * $weightperitem; 
     alert($weightperpal); 
     if($totalitems !== '' && $itemspb !== '' && $boxespp !== ''){ 
      if($totalpals == Infinity){ 
       $('#palletsF'+number).val('0'); 
      }else{ 
       $('#palletsF'+number).val($totalpals); 
      } 

      $('#weightppF'+number).val($weightperpal); 
     } 
    } 

提前感謝!

+0

請記住在'parseFloat(...)'中包裝'$(...)。val()',以便將值轉換爲數字。 – JCOC611 2015-02-11 21:31:16

+1

也使用'console.log()'而不是'alert()'來查看對象 – 2015-02-11 21:32:03

回答

2

$('#totalitF'+number)這樣的選擇器都不會起作用,因爲您不會將編號附加到您的ID。相反的:

id="totalitF" no="'+ lineNum +'"/>' 

它應該是:

id="totalitF'+ lineNum +'" data-no="'+ lineNum +'"/>' 

你不應該創建自己的屬性。如果您想要將其他屬性添加到元素,請使用data-屬性。然後在jQuery中,您可以訪問此爲

$(this).data('no') 

以獲取該值。在您的其他功能中使用此代替$(this).attr('no')

+0

中的內容,所以我應該刪除輸入中額外的'no'屬性,並且只是通過id =「totalitF' + lineNum +'「/>'? – idelara 2015-02-11 23:00:03

+0

其實,我發現你在其他函數中使用'.attr('no')',所以你需要這兩個函數。看到我更新的答案。 – Barmar 2015-02-11 23:12:02

相關問題