简体   繁体   English

我在javascript中使用数组函数时遇到问题

[英]I have an issue with making an array function in javascript

I made this simple shopping cart where you select quantity from a dropwdown list and these numbers get added and you get a total number for all the products and a total number for the product itself. 我制作了这个简单的购物车,您从下拉列表中选择数量,然后将这些数字相加,就可以得到所有产品的总数和产品本身的总数。

But i want it write the quantity with price and the name and write it in a div placed on the site so that it is easy for the user to see that he added a product. 但是我希望它用价格和名称写出数量,并将其写在放置在站点上的div中,这样用户可以很容易地看到他添加了产品。 I made the calculating part by adding the products one by one, as i did not array them. 我通过逐个添加乘积来构成计算部分,因为我没有对它们进行排列。 But for this next part i just didn't find a way that suited my needs. 但是对于下一部分,我只是找不到适合我需求的方法。 This is just from my test page , so i only have a few values , but in the other page i have over 70 listed just like these: 这只是来自我的测试页面,所以我只有几个值,但是在另一页面中,我列出了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>

There is no newArray function in JavaScript by default. 默认情况下,JavaScript中没有newArray函数。

Change 更改

var myArray = newArray();

to

var myArray = [];

or 要么

var myArray = new Array();
// Note space ---^

The form using [] is generally preferred. 通常首选使用[]的形式。 In addition to being more concise, it's not susceptible to someone redefining the Array constructor function. 除了更加简洁之外,它不容易受到重新定义Array构造函数的影响。

Note that you can initialize the array with the elements as well if you like: 请注意,如果您愿意,也可以使用元素初始化数组:

var myArray = [
    document.form.sum2.value,
    document.form.prod0.value,
    document.form.prod1.value,
    document.form.prod2.value,
    document.form.prod3.value,
    document.form.prod4.value
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM