2013-01-07 33 views
0

我有這樣的功能:更新值

$("#inc<?php echo"$key"; ?>").click(function(){ 

    prod_id = "<?php echo"$key"; ?>"; 
    $.ajax({ 
    type: "POST", 
    url: "updateproduct2cart.php", 
    data: "itemid="+prod_id+"&act=update&qty=up" 
    }); 

    var currentValue = $("#inc<?php echo"$key"; ?>").closest('#SCprodsB').next('#SCprodsF').find('#totalCommande').html(); 
    currentValue = parseFloat(currentValue); 

    $(":text[name='qty<?php echo"$key"; ?>']").val(Number($(":text[name='qty<?php echo"$key"; ?>']").val()) + 1); 

    var totalItems = parseFloat($("#qty<?php echo"$key"; ?>").val()); 
    var priceItem = parseFloat(<?php echo"$priceProduct"; ?>).toFixed(2); 
    var totalValueUp = parseFloat(priceItem * totalItems).toFixed(2); 
    $("#subtotal<?php echo"$key"; ?>").html("&#128; "+ totalValueUp); 

    /* PART BELOW IS NOT WORKING PROPERLY*/ 

    var numItems = parseFloat($(":text[name='qty<?php echo"$key"; ?>']").val()); 
    var totalPriceItems = parseFloat(priceItem * numItems).toFixed(2); 
    var newValue = parseFloat(currentValue + totalPriceItems).toFixed(2); 
    $("#inc<?php echo"$key"; ?>").closest('#SCprodsB').next('#SCprodsF').find('#totalCommande').html(newValue); 

    }); 

這是HTML部分:

<table id="SCproduits" summary="commande"> 
    <caption></caption> 
    <thead> 
     <tr> 
      <th scope="col">--</th> 
      <th scope="col">--</th> 
      <th scope="col">--</th> 
      <th scope="col">--</th> 
      <th scope="col">--</th> 
      <th scope="col">--</th> 
      <th scope="col">--</th> 
      <th scope="col">--</th> 
     </tr> 
    </thead> 
    <tbody id="SCprodsB"> 
     <tr id="prodItemi3" name="prodItemi3"> 
      <tr id="prodItemi4" name="prodItemi4"> 
       <th id="prodItemi4" scope="row">xxxxxxxxx</th> 
       <td>xxx</td> 
       <td> 
        <td> 
         <button id="deci4" class="SCbutton">-</button> 
         <input id="qtyi4" class="SCinput3" type="text" size="2" value="1" 
         name="qtyi4"> 
         <button id="inci4" class="SCbutton">+</button> 
        </td> 
        <td> 
         <td> 
          <td> 
           <td> 
      </tr> 
    </tbody> 
    <tfoot id="SCprodsF"> 
     <tr> 
      <th scope="row"></th> 
      <td colspan="7">€ <span id="totalCommande">xxx</span> 

      </td> 
     </tr> 
    </tfoot> 
</table> 

我不能得到(跨度ID =」 totalCommande「)在使用(inci4按鈕)時使用正確的總值進行更新。任何想法 ?我走錯路了嗎?由於

+0

那麼究竟發生了什麼?它沒有更新嗎?還是它不會給你你期望的價值? –

+0

它更新,​​但不是正確的值 – ViKKKing

+0

所以...你期望什麼,你會得到什麼?什麼是中間值? –

回答

0

你的問題可能出在這裏:

var totalPriceItems = parseFloat(priceItem * numItems).toFixed(2); 
var newValue = parseFloat(currentValue + totalPriceItems).toFixed(2); 

使用.toFixed正在轉換的數字回字符串。然後,當您將currentValue(一個數字)添加到totalPriceItems(一個字符串)時,您最終將執行字符串連接而不是加法。

因此,如果currentValue = 1totalPriceItems = "2",你會得到"12"(一個字符串),而你回答而不是3

+0

+1你是對的,沒有注意到,甚至沒有知道,並沒有在文檔中找到它:) 仍然,我沒有得到正確的價值,但我會工作,並讓你知道 – ViKKKing

+0

完美! tyvm的提示,它現在工作;) – ViKKKing