【问题标题】:Delete Item in Javascript在 Javascript 中删除项目
【发布时间】:2017-12-01 06:25:34
【问题描述】:

第一个问题是我在删除功能方面需要帮助。我已经尝试过拼接方法,但我仍然无法得到它。当您单击删除项目按钮时,必须删除该行。 第二个问题 ii 已经在输入表单中放入了 required 属性,但是当字段为空时它仍然提交表单。

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>

【问题讨论】:

  • “我已经在输入表单中放入了 required 属性,但是当字段为空时它仍然提交表单” - 你是如何提交表单的?看起来您的表单元素不包含任何提交按钮,并且您动态创建的提交按钮被添加到表单外的元素中。
  • @nnnnnn——可以通过在输入元素上按下回车键来提交表单。以编程方式提交的表单中经常被遗忘的功能。 :-(
  • @RobG - 我认为这只适用于具有单个输入元素的表单?

标签: javascript html arrays javascript-objects dom-events


【解决方案1】:

首先,您必须传递正确的 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);
        }
    }
}

【讨论】:

  • 首先,添加您想要的任何值的字段。其次添加没有任何值的字段。然后删除您添加的第二个,然后删除第一个。第一个还在
  • @charles 抱歉我错过了,我想现在是正确的。
  • 谢谢先生。现在可以了。对了,如果值为空怎么不加到表中?
  • @charles I have made changes 如果表为空,脚本现在会删除表。
  • 是的,它现在正在工作。但我还有一个问题。如果值为空,如何不添加到表中。如果输入的值为空,它会提醒用户。谢谢迪瓦斯
【解决方案2】:

您的主要问题是您没有将产品 id 传递给 delete 函数,并且您期望它在它上面。

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

我已经完成了这个 sn-p。我已将产品 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>

希望这会有所帮助!

【讨论】:

  • 点击Delete Item时似乎不会删除行。
  • 你运行我的 sn-p 了吗?当您单击删除项目时,它会删除行
  • 啊,我确实运行了它,但是它依赖于有一个 ID。我在不输入 ID 的情况下添加行。这不是您的解决方案本身的问题,更多的是 OP 的代码在添加行之前未检查 ID 的问题。赞成。
  • 是的,你是对的...应该检查 id 是否已插入!但正如你所说,那是另一回事:)
  • 先生,当没有添加值(表格为空白)时,无法删除。
【解决方案3】:

问题在于您正在修改products 的数组,而实际上并未更改页面上显示的HTML。

如果你添加代码删除指定的table元素,我相信问题会解决。

例如: 您可以在创建每个表时为其附加一个对应于 product_id 的 id,然后删除具有该 id 的表。

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

【讨论】:

【解决方案4】:

您的点击处理程序正在调用 deleteProduct(),它将调用您的函数而不传递 product_id 值作为参数,因此您的 if (products[i].product_id == product_id) 永远不会被评估为 true。

试试这个:

 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 属性,并将 &lt;p id="demo"&gt;&lt;/p&gt; 移动到表单中。

【讨论】:

  • 您应该将代码格式化为代码。在这种情况下,我建议在您的代码周围使用反引号 `
  • TY! Clonkex 不知道我可以 +1
【解决方案5】:

您必须将 delete button typesubmit 更改为 button 并且必须为 deleteProduct 函数添加 products[i].product_id 参数

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>";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-13
    • 2015-04-14
    • 2014-05-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    相关资源
    最近更新 更多