我有两个问题。提交(添加)产品信息后,我想将输入字段设为空。第二个问题是使编辑功能工作。它不工作。我不知道该怎么做。希望你能帮上忙。
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>
2条答案
按热度按时间vjhs03f71#
您需要向“editProduct”函数传递参数以使其工作。
然后你需要小心,如果一些输入是空的,把一些默认值的例子:
你觉得可以吗?
ifsvaxew2#
编辑单元格
在包含用户单击的编辑按钮的
<tr>
的前4个<td>
上,将["contenteditable"]
属性从/切换到true
/false
。清除
<input>
s只需将按钮的
[type]
属性从"button"
更改为"reset"
。示例中有详细注解