Javascript添加和编辑功能

时间:2017-06-27 02:47:10

标签: javascript html javascript-events javascript-objects

我有两个问题。在我提交(添加)产品信息后,我想将输入字段留空。第二个问题是使编辑功能起作用。它不起作用。我不知道如何做到这一点。希望你能帮忙。感谢。

var qtyTotal = 0;
    var priceTotal = 0;

    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;    
        //priceTotal = priceTotal + parseInt(price);
        //document.getElementById("priceTotals").innerHTML=priceTotal;
        var table=document.getElementById("results");
        var row=table.insertRow(-1);
        var cell1=row.insertCell(0);
        var cell2=row.insertCell(1);
        var cell3=row.insertCell(2);
        var cell4=row.insertCell(3);
        var cell5=row.insertCell(4);
        var cell6=row.insertCell(5);
        cell1.innerHTML=productID;
        cell2.innerHTML=product_desc;
        cell3.innerHTML=qty;        
        cell4.innerHTML=price;  
        cell5.innerHTML=' <button type="button" onClick="editProduct();"/>Edit</button>'; 
        cell6.innerHTML ='<button type="button" onClick="deleteProduct(this);">Delete</button>';             
}

    function editProduct(){
        

        document.getElementById("productID").value = productID;
        document.getElementById("product_desc").value = product_desc;
        document.getElementById("quantity").value = qty;
        document.getElementById("price").value = price;      
 }
    function deleteProduct(node){    
    r=node.parentNode.parentNode;
    r.parentNode.removeChild(r);
}
<!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" onclick="addProduct();" value="Add New Product" >
</form>
<br>

<table id="results">
    <thead>
    <tr>
        <th scope="col" width="50">Product ID</th>
        <th scope="col" width="100">Product Description</th>
        <th scope="col" width="50">Quantity</th>
        <th scope="col" width="50">Price</th>
        <th scope="col" width="50">Action</th>
    </tr>
    </thead>
</table>
<!--<table id="resultTotals" width="360">
<tr>
    <td scope="col" width="120">Totals</td>
    <td scope="col" width="120"><div id="qtyTotals"></div></td>
    <td scope="col" width="120"><div id="priceTotals"></div></td>
</tr>
</table>*/-->

</body>
</html>

1 个答案:

答案 0 :(得分:0)

您需要将参数传递到&#34; editProduct&#34;使其有效的功能。

cell5.innerHTML=' <button type="button" onClick="editProduct(\''+productID+'\', \''+product_desc+'\', \''+qty+'\', \''+price+'\');"/>Edit</button>'; 

function editProduct(productID, product_desc, qty, price){
    document.getElementById("productID").value = productID;
    document.getElementById("product_desc").value = product_desc;
    document.getElementById("quantity").value = qty;
    document.getElementById("price").value = price;      
 }

然后,如果某些输入为空,则需要小心,通过示例放置一些默认值:

var productID = document.getElementById("productID").value || "none";
var qty = document.getElementById("quantity").value || 0;

你可以吗?