2017-06-29 48 views
2

我真的需要我的購物車功能幫助。問題是,當我第二次在購物車中添加產品時,它會替換第一個中的值。它應該顯示在另一行。請幫幫我。謝謝。替換第一行中的值

var qtyTotal = 0; 
 
    var priceTotal = 0; 
 
    var products = []; 
 

 

 
    function addProduct() { 
 
     var productID = document.getElementById("productID").value; 
 
     var product_desc = document.getElementById("product_desc").value; 
 
     var qty = document.getElementById("quantity").value; 
 
     // qtyTotal = qtyTotal + parseInt(qty); 
 
     //document.getElementById("qtyTotals").innerHTML=qtyTotal; 
 
     var price = document.getElementById("price").value; 
 

 
     var newProduct = { 
 

 
      product_id : null, 
 
      product_desc : null, 
 
      product_qty : 0, 
 
      product_price : 0.00, 
 
     }; 
 
     newProduct.product_id = productID; 
 
     newProduct.product_desc = product_desc; 
 
     newProduct.product_qty = qty; 
 
     newProduct.product_price = price; 
 

 

 
     products.push(newProduct); 
 

 
     //console.log("New Product " + JSON.stringify(newProduct)) 
 
     //console.log("Products " + JSON.stringify(products)) 
 

 
     var html = "<table border='1|1' >"; 
 
     html+="<td>Product ID</td>"; 
 
     html+="<td>Product Description</td>"; 
 
     html+="<td>Quantity</td>"; 
 
     html+="<td>Price</td>"; 
 
     html+="<td>Action</td>"; 
 
     for (var i = 0; i < products.length; i++) { 
 
     html+="<tr>"; 
 
     html+="<td>"+products[i].product_id+"</td>"; 
 
     html+="<td>"+products[i].product_desc+"</td>"; 
 
     html+="<td>"+products[i].product_qty+"</td>"; 
 
     html+="<td>"+products[i].product_price+"</td>"; 
 
     html+="<td><button type='submit' onClick='deleteProduct(\""+products[i].product_id +"\", this);'/>Delete Item</button> &nbsp <button type='submit' onClick='addCart(\""+products[i].product_id +"\", this);'/>Add to Cart</button></td>"; 
 
     html+="</tr>"; 
 
    } 
 
     html+="</table>"; 
 
     document.getElementById("demo").innerHTML = html; 
 

 
     document.getElementById("resetbtn").click()    
 
} 
 
    function deleteProduct(product_id, e) { 
 
     e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode); 
 
     for(var i = 0; i < products.length; i++) { 
 
      if (products[i].product_id == product_id) { 
 
       // DO NOT CHANGE THE 1 HERE 
 
       products.splice(i, 1); 
 
      } 
 
     } 
 
    } 
 

 
    function addCart(product_id){ 
 
     var html = "<table border='1|1'>"; 
 
     html+="<td>Product ID</td>"; 
 
     html+="<td>Product Description</td>"; 
 
     html+="<td>Quantity</td>"; 
 
     html+="<td>Price</td>"; 
 
     html+="<td>Total</td>"; 
 
     html+="<td>Action</td>"; 
 
     for (var i = 0; i < products.length; i++) { 
 
      if (products[i].product_id == product_id) { 
 
      products[i].product_qty = parseInt(products[i].product_qty) + 1; 
 
      html+="<tr>"; 
 
      html+="<td>"+products[i].product_id+"</td>"; 
 
      html+="<td>"+products[i].product_desc+"</td>"; 
 
      html+="<td>"+products[i].product_qty+"</td>"; 
 
      html+="<td>"+products[i].product_price+"</td>"; 
 
      html+="<td>"+parseInt(products[i].product_price)*parseInt(products[i].product_qty)+"</td>"; 
 
      html+="<td><button type='submit' onClick='subtractQuantity(\""+products[i].product_id +"\", this);'/>Subtract Quantity</button></td>"; 
 
      html+="</tr>"; 
 
     }  
 
    } 
 
      html+="</table>"; 
 
      document.getElementById("demo2").innerHTML = html; 
 

 
    } 
 
    function subtractQuantity(product_id) 
 
    { alert(product_id); 
 
    for(var i = 0; i < products.length; i++) { 
 
     if (products[i].product_id == product_id & products[i].product_qty >= 1) { 
 
      products[i].product_qty = parseInt(products[i].product_qty) - 1; 
 
     } 
 

 
     if (products[i].product_id == 0) { 
 
      removeItem(products[i].product_id); 
 
     } 
 
     console.log("Products " + JSON.stringify(products)); 
 
     
 
    } 
 
} 
 

 
function removeItem(product_id) { 
 
    for(var i = 0; i < products.length; i++) { 
 
     if (products[i].product_id == product_id) { 
 
      // DO NOT CHANGE THE 1 HERE 
 
      products.splice(i, 1); 
 
     } 
 
    } 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>Shopping Cart Pure Javascript</title> 
 
</head> 
 
<body> 
 
<form name="order" id="order"> 
 
    <table> 
 
     <tr> 
 
      <td> 
 
       <label for="productID">Product ID:</label> 
 
      </td> 
 
      <td> 
 
       <input id="productID" name="product" type="text" size="28" required/> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> 
 
       <label for="product">Product Desc:</label> 
 
      </td> 
 
      <td> 
 
       <input id="product_desc" name="product" type="text" size="28" required/> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> 
 
       <label for="quantity">Quantity:</label> 
 
      </td> 
 
      <td> 
 
       <input id="quantity" name="quantity" width="196px" required/> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> 
 
       <label for="price">Price:</label> 
 
      </td> 
 
      <td> 
 
       <input id="price" name="price" size="28" required/> 
 
      </td> 
 
     </tr> 
 
    </table> 
 
    <input type="reset" name="reset" id="resetbtn" class="resetbtn" value="Reset" /> 
 
    <input type="button" id="btnAddProduct" onclick="addProduct();" value="Add New Product" > 
 

 

 
</form> 
 
<br> 
 
<p id="demo"></p> <br/> 
 
<h2> Shopping Cart </h2> 
 
<p id="demo2"></p> 
 
</body> 
 
</html>

+0

我編輯了自己的代碼,並用JavaScript的幫助下解決您的問題。 – RJParikh

回答

0
document.getElementById("demo2").innerHTML = html; 

每次,該函數被調用,你改變的「DEMO2」的innerHTML,而你需要做的是追加到它。使用

document.getElementById("demo2").innerHTML += html; 

而且,不使用的innerHTML property.It破壞從而殺死引用鏈接到對象事件監聽器或類似的東西是個好主意。希望能幫助到你 !

+0

它增加了產品ID等的另一個標題... – charles

+0

然後將這些「標題」欄移出並保留在html中。只在需要時附加表格行。您可以使用JS來隱藏這些表頭,直到您追加一行。 –

+0

您可以編輯我的代碼先生並在此處顯示嗎?謝謝你,先生 – charles

1

查看下方的代碼。我已經改變它與JavaScript。

var qtyTotal = 0; 
 
    var priceTotal = 0; 
 
    var products = []; 
 

 

 
    function addProduct() { 
 
     var productID = document.getElementById("productID").value; 
 
     var product_desc = document.getElementById("product_desc").value; 
 
     var qty = document.getElementById("quantity").value; 
 
     // qtyTotal = qtyTotal + parseInt(qty); 
 
     //document.getElementById("qtyTotals").innerHTML=qtyTotal; 
 
     var price = document.getElementById("price").value; 
 

 
     var newProduct = { 
 

 
      product_id : null, 
 
      product_desc : null, 
 
      product_qty : 0, 
 
      product_price : 0.00, 
 
     }; 
 
     newProduct.product_id = productID; 
 
     newProduct.product_desc = product_desc; 
 
     newProduct.product_qty = qty; 
 
     newProduct.product_price = price; 
 

 

 
     products.push(newProduct); 
 

 
     //console.log("New Product " + JSON.stringify(newProduct)) 
 
     //console.log("Products " + JSON.stringify(products)) 
 

 
     var html = "<table border='1|1' >"; 
 
     html+="<td>Product ID</td>"; 
 
     html+="<td>Product Description</td>"; 
 
     html+="<td>Quantity</td>"; 
 
     html+="<td>Price</td>"; 
 
     html+="<td>Action</td>"; 
 
     for (var i = 0; i < products.length; i++) { 
 
     html+="<tr>"; 
 
     html+="<td>"+products[i].product_id+"</td>"; 
 
     html+="<td>"+products[i].product_desc+"</td>"; 
 
     html+="<td>"+products[i].product_qty+"</td>"; 
 
     html+="<td>"+products[i].product_price+"</td>"; 
 
     html+="<td><button type='submit' onClick='deleteProduct(\""+products[i].product_id +"\", this);'/>Delete Item</button> &nbsp <button type='submit' onClick='addCart(\""+products[i].product_id +"\", this);'/>Add to Cart</button></td>"; 
 
     html+="</tr>"; 
 
    } 
 
     html+="</table>"; 
 
     document.getElementById("demo").innerHTML = html; 
 

 
     document.getElementById("resetbtn").click()    
 
} 
 
    function deleteProduct(product_id, e) { 
 
     e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode); 
 
     for(var i = 0; i < products.length; i++) { 
 
      if (products[i].product_id == product_id) { 
 
       // DO NOT CHANGE THE 1 HERE 
 
       products.splice(i, 1); 
 
      } 
 
     } 
 
    } 
 

 
    function addCart(product_id){ 
 
     var html = ''; 
 
     var ele = document.getElementById("demo2");  
 
     if(ele.innerHTML == '') 
 
     { 
 
     html+="<table id='tblCart' border='1|1'>"; 
 
     html+="<tr><td>Product ID</td>"; 
 
     html+="<td>Product Description</td>"; 
 
     html+="<td>Quantity</td>"; 
 
     html+="<td>Price</td>"; 
 
     html+="<td>Total</td>"; 
 
     html+="<td>Action</td></tr>"; 
 
     } 
 
     for (var i = 0; i < products.length; i++) { 
 
      if (products[i].product_id == product_id) { 
 
      products[i].product_qty = parseInt(products[i].product_qty) + 1; 
 
      html+="<tr>"; 
 
      html+="<td>"+products[i].product_id+"</td>"; 
 
      html+="<td>"+products[i].product_desc+"</td>"; 
 
      html+="<td>"+products[i].product_qty+"</td>"; 
 
      html+="<td>"+products[i].product_price+"</td>"; 
 
      html+="<td>"+parseInt(products[i].product_price)*parseInt(products[i].product_qty)+"</td>"; 
 
      html+="<td><button type='submit' onClick='subtractQuantity(\""+products[i].product_id +"\", this);'/>Subtract Quantity</button></td>"; 
 
      html+="</tr>"; 
 
     }  
 
    } 
 
     
 
     if(ele.innerHTML == '') 
 
     { 
 
     html+= "</table>"; 
 
     ele.innerHTML = html; 
 
     } 
 
     else 
 
     { 
 
     document.getElementById("tblCart").innerHTML += html;  
 
     }  
 
    } 
 
    
 
    function subtractQuantity(product_id) 
 
    { alert(product_id); 
 
    for(var i = 0; i < products.length; i++) { 
 
     if (products[i].product_id == product_id & products[i].product_qty >= 1) { 
 
      products[i].product_qty = parseInt(products[i].product_qty) - 1; 
 
     } 
 

 
     if (products[i].product_id == 0) { 
 
      removeItem(products[i].product_id); 
 
     } 
 
     console.log("Products " + JSON.stringify(products)); 
 
     
 
    } 
 
} 
 

 
function removeItem(product_id) { 
 
    for(var i = 0; i < products.length; i++) { 
 
     if (products[i].product_id == product_id) { 
 
      // DO NOT CHANGE THE 1 HERE 
 
      products.splice(i, 1); 
 
     } 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>Shopping Cart Pure Javascript</title> 
 
</head> 
 
<body> 
 
<form name="order" id="order"> 
 
    <table> 
 
     <tr> 
 
      <td> 
 
       <label for="productID">Product ID:</label> 
 
      </td> 
 
      <td> 
 
       <input id="productID" name="product" type="text" size="28" required/> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> 
 
       <label for="product">Product Desc:</label> 
 
      </td> 
 
      <td> 
 
       <input id="product_desc" name="product" type="text" size="28" required/> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> 
 
       <label for="quantity">Quantity:</label> 
 
      </td> 
 
      <td> 
 
       <input id="quantity" name="quantity" width="196px" required/> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> 
 
       <label for="price">Price:</label> 
 
      </td> 
 
      <td> 
 
       <input id="price" name="price" size="28" required/> 
 
      </td> 
 
     </tr> 
 
    </table> 
 
    <input type="reset" name="reset" id="resetbtn" class="resetbtn" value="Reset" /> 
 
    <input type="button" id="btnAddProduct" onclick="addProduct();" value="Add New Product" > 
 

 

 
</form> 
 
<br> 
 
<p id="demo"></p> <br/> 
 
<h2> Shopping Cart </h2> 
 
<p id="demo2"></p> 
 
</body> 
 
</html>

+0

先生,我被告知只能使用Javascript,你能幫我解決這個問題嗎?非常感謝你 – charles

+0

你需要爲jquery添加一個腳本,它很難用javascript實現。內置的函數很有用 – RJParikh

+0

是的,先生我明白,但我的導師告訴我練習我的JavaScript,所以沒有jquery,你能幫我用那個純JavaScript寫的嗎?謝謝你太太先生 – charles