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();'/>Delete Item</button> &nbsp <button type='submit' onClick='addCart();'/>Add to Cart</button></td>"; 
 
     html+="</tr>"; 
 
    } 
 
     html+="</table>"; 
 
     document.getElementById("demo").innerHTML = html; 
 

 
     document.getElementById("resetbtn").click()    
 
} 
 
    function deleteProduct(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> 
 
</body> 
 
</html>

+1

*「我已經把需要的屬性輸入形式,但它依然提交表單時,該字段爲空」 * - 您是如何提交表單?看起來您的表單元素不包含任何提交按鈕,並且您動態創建的提交按鈕將添加到表單外的元素。 – nnnnnn

+0

@ nnnnnn-forms可以通過按下輸入元素焦點輸入來提交。在編程式提交的表單中經常遺忘的一個特徵。 :-( – RobG

+0

@RobG - 我認爲只適用於帶有單個輸入元素的表單嗎? – nnnnnn

回答

2

首先,你必須通過正確的product_id刪除功能。您還需要知道要刪除哪個元素,因此您可以發送當前點擊該元素的元素,然後訪問其父節點以將其刪除。你可以用下面的代碼替換你的腳本:

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 id='products-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();'/>Add to Cart</button></td>"; 
     html+="</tr>"; 
    } 
     html+="</table>"; 
     document.getElementById("demo").innerHTML = html; 

     document.getElementById("resetbtn").click()    
} 
    function deleteProduct(product_id, e) { 

    var pTbody = e.parentNode.parentNode.parentNode; 
var pTable = pTbody.parentNode; 
if((pTbody.children).length === 2) 
    pTable.parentNode.removeChild(pTable); 
else 
    pTbody.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); 
     } 
    } 
} 
+0

當您刪除 – charles

+0

@charles時,仍然存在錯誤錯誤是什麼? – Dij

+0

首先,添加你想要的任何值的字段。其次添加沒有任何價值的領域。然後刪除已添加的第二個,然後刪除第一個。第一個仍然存在 – charles

1

的問題是,你正在修改的products陣列而無需實際更改HTML顯示在頁面上。

如果您添加了代碼以刪除指定的table元素,我相信問題會得到解決。

例如: 您可以在每個表創建時附加一個對應於product_id的標識,然後使用該標識刪除該表。

var remove = document.getElementById(product_id); 
remove.parentElement.removeChild(remove); 
+0

我可以請求代碼嗎,先生? – charles

+0

我會添加一些代碼@charles – clabe45

+0

我已經知道了sir – charles

0

你的點擊處理程序調用deleteProduct(),這將調用你的函數沒有通過的product_id值作爲參數,以便您if (products[i].product_id == product_id)永遠不會評估是正確的。

試試這個:

html+="<td><button type='submit' onClick='deleteProduct(\""+products[i].product_id +"\");'/>Delete Item</button> &nbsp; <button type='submit' onClick='addCart();'/>Add to Cart</button></td>"; 

至於必填字段請確保您有type屬性您所有的輸入設置和移動<p id="demo"></p>表單內。

+1

你應該將代碼格式化爲代碼,在這種情況下,我建議在你的周圍使用反引號'\''代碼 – Clonkex

+0

TY!Clonkex不知道我可以那樣+1 –

+0

不用擔心的人:D – Clonkex

1

您的主要問題是您沒有將產品ID傳遞到刪除功能,並且您正在期待它。

此外,您可以刪除HTML元素本身。

我已經完成了這個片段。我已將產品ID添加爲表格行元素的ID,因此在您的刪除功能中,您可以將其刪除以查找該ID。

此外,如果刪除的項目是最後的表,表也會被刪除(我已經添加了一個ID,表,以便您可以輕鬆地將其刪除)

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 id='products-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 id='"+products[i].product_id+"'>"; 
 
     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+");'/>Delete Item</button> &nbsp <button type='submit' onClick='addCart();'/>Add to Cart</button></td>"; 
 
     html+="</tr>"; 
 
    } 
 
    
 
    html+="</table>"; 
 
    document.getElementById("demo").innerHTML = html; 
 

 
    document.getElementById("resetbtn").click()    
 
} 
 

 
function deleteProduct(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); 
 
      var element = document.getElementById(product_id); 
 
      var tableElement = document.getElementById('products-table'); 
 
      
 
      if(!products.length) 
 
       tableElement.parentNode.removeChild(tableElement); 
 
      else 
 
       tableElement.removeChild(element); 
 
     } 
 
    } 
 
}
<!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> 
 
</body> 
 
</html>

希望這會有所幫助!

+0

點擊'刪除項目'時,這似乎不會刪除行 – Clonkex

+0

您是否運行了我的代碼段?你點擊刪除項目 –

+0

啊,我確實運行了它,但它依賴於有一個ID,我在添加行時沒有輸入ID,這不是你的問題解決方案本身,更多的是OP的代碼在添加行之前沒有檢查ID的問題。 Upvoted。 – Clonkex

0

您必須更改刪除submitbutton typebutton,你必須添加products[i].product_id paramater爲功能

html+="<td><button type='button' onClick='deleteProduct("+products[i].product_id+");'/>Delete Item</button> &nbsp <button type='submit' onClick='addCart();'/>Add to Cart</button></td>";