我制作了这个简单的购物车,您可以从下拉列表中选择数量,然后添加这些数字,然后您会获得所有产品的总数和产品本身的总数。
但我希望它用价格和名称写下数量,并将其写在放置在网站上的 div 中,以便用户很容易看到他添加了产品。我通过将产品一一添加来进行计算部分,因为我没有排列它们。但是对于下一部分,我只是没有找到适合我需要的方法。这只是来自我的测试页面,所以我只有几个值,但在另一个页面中,我列出了 70 多个,如下所示:
<div id="radioAlert">
<script type="text/javascript">
//calculator
function updatesum() {
document.form.sum.value =
(document.form.sum0.value -0) *99 +
(document.form.sum1.value -0) *99 +
(document.form.sum2.value -0) *99;
document.form.smu.value =
(document.form.prod0.value -0)*99 +
(document.form.prod1.value -0) *99 +
(document.form.prod2.value -0) *99 +
(document.form.prod3.value -0) *99 +
(document.form.prod4.value -0) *99;
document.form.totalsum.value =
(document.form.sum.value -0) +
(document.form.smu.value -0); //not a typo i was just lazy with the name
//arrays
//This one i did not get to work.
var myArray = newArray();
myArray[0]=document.form.sum2.value;
myArray[1]=document.form.prod0.value;
myArray[2]=document.form.prod1.value;
myArray[3]=document.form.prod2.value;
myArray[4]=document.form.prod3.value;
myArray[5]=document.form.prod4.value;
for (var i = 0; i < myArray.length; i++) {
alert(myArray[i]);
}
//this part works but i want it to add its value only once and then replace its value if its updated again, if its 0 i want it to be completely removed.
var prod1 = document.form.sum2.value;
var prod2 = document.form.sum1.value;
var radioAlert = document.getElementById("radioAlert");
var radioAlert2 = document.getElementById("radioAlert");
if (document.form.sum2.value>0) {
//document.write("pilotjakke pelsforet small " + prod1 + " stk");
radioAlert.innerHTML += ("pilotjakke pelsforet small " + prod1 + " stk");
alert("pilotjakke pelsforet medium " + prod1 + " stk");
}
if (document.form.sum1.value>0) {
//document.write("pilotjakke pelsforet small " + prod1 + " stk");
radioAlert2.innerHTML += ("pilotjakke pelsforet medium " + prod2 + " stk");
}
}
</script>
</div>