gpt4 book ai didi

Javascript - 函数结束时数组清空值

转载 作者:行者123 更新时间:2023-11-30 06:44:29 25 4
gpt4 key购买 nike

我有一个函数可以计算订单的总报价,然后将输出提醒给用户。我还希望将总报价存储在一个数组中,以便可以调用一个单独的函数来显示数组中的所有值(显示自页面加载以来的所有报价)。据我所知,随着函数的结束,数组丢失了函数插入的值,而且我一直在玩弄数组的范围,但没有任何乐趣,希望能在正确的方向上插入一下。

<form>

<table id="kit" cellpadding="2" cellspacing="5">

<th colspan="2" align="center"><h3>Purchase Shirts (Coming Soon)</h3></th>

<tr><td class="titles">Size</td>
<td class="titles">Qty</td></tr>

<tr><td>Small (£10)</td>
<td><input type="text" size="3" maxlength="5" name="small" /></td>

<tr><td>Medium (£12)</td>
<td><input type="text" size="3" maxlength="5" name="medium" /></td>

<tr><td>Large (£15)</td>
<td><input type="text" size="3" maxlength="5" name="large" /></td>

<tr><td>X-Large (£20)</td>
<td><input type="text" size="3" maxlength="5" name="xlarge" /></td>

<tr><td colspan="2" align="center">
<input class="submit" type="submit" onClick="return calculateShirts(this)" value="Get Quote" /></td>

</tr>
</table>
</form>

JavaScript-------------

var totalQuotes = [1,2]; //Initialise the global array with example values


function calculateShirts(form) //Function to calculate shirt 'quote'
{
//Assign Prices for Each Shirt Size
var sml = 10;
var med = 12;
var lge = 15;
var xl = 20;

//Save the user inputs as variables
var smlQu = form.small.value;
var medQu = form.medium.value;
var lgeQu = form.large.value;
var xlQu = form.xlarge.value;

//Multiply the Price by the User Input and save as variable
var smlQuote = (sml * smlQu);
var medQuote = (med * medQu);
var lgeQuote = (lge * lgeQu);
var xlQuote = (xl * xlQu);

//Add the calculated values together to get the total price
var finalQuote = (smlQuote + medQuote + lgeQuote + xlQuote);

//Create an array containing the quotes
var arrayQuote = [smlQuote, medQuote, lgeQuote, xlQuote, finalQuote];

//Variable containing the formatted output of quotes
var output = "Your Kit Quote \n\n Small - £" + arrayQuote[0] + "\n" + "Medium - £" + quoteArray[1] + "\n" + "Large - £" + quoteArray[2] + "\n" + "X-Large - £" + quoteArray[3] + "\n\n" + "Total - £" + quoteArray[4];

//Display the output variable in a popup box
alert(output);


totalQuotes.push(finalQuote);

alert(totalQuotes); //This alert does show the calculated value
return false;

}



function printQuotes() //Function called on to display array values
{

for (i in totalQuotes) {
alert(totalQuotes[i]);

//The calculated value is no longer in the array

}

}

最佳答案

这对我来说很好用。那里有一些语法错误,

var output = "Your Kit Quote \n\n Small - £" + arrayQuote[0] + "\n" + "Medium - £" + quoteArray[1] + "\n" + "Large - £" + quoteArray[2] + "\n" + "X-Large - £" + quoteArray[3] + "\n\n" + "Total - £" + quoteArray[4];

您首先引用 arrayQuote,然后更改为不存在的 quoteArray。在此处发布问题时不确定这是否只是一个错字。

鉴于我硬编码的这些值:

var smlQu = 2;
var medQu = 1;
var lgeQu = 3;
var xlQu = 5;

alert(totalQuotes); // returns 1,2,177

printQuotes(); // returns alerts with 1 then 2 then 177

要停止刷新表单,请将此行添加到 calculateShirts() 的底部:

return false;

并更改提交时的表单:

onsubmit="calculateShirts(this)" to onsubmit="return calculateShirts(this)"

如果您仍想运行 print 方法,只需在返回 false 之前调用它即可。

关于Javascript - 函数结束时数组清空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8226886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com