【问题标题】:Javascript - Array emptying value at close of functionJavascript - 函数结束时数组清空值
【发布时间】:2011-11-22 12:33:11
【问题描述】:

我有一个函数可以计算订单的总报价,然后将输出提醒用户。我还希望将总报价存储在一个数组中,以便可以调用一个单独的函数,该函数将显示数组中的所有值(显示自页面加载以来的所有引号)。据我所知,随着函数的结束,数组失去了函数推入的值,我已经玩弄了数组的范围,没有任何乐趣,并且希望朝着正确的方向轻推。

<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

    }

}

【问题讨论】:

  • 不要使用for in 循环数组,使用for( idx; test; inc )for in 用于迭代对象的属性。
  • 变量arrayQuote似乎指的是一个你没有提到的全局。
  • for in 只是我忘记删除的小提琴之一,抱歉。我最初只是一次提醒整个数组。
  • 我不明白你的意思是什么?这一切都在我身边

标签: javascript arrays function


【解决方案1】:

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

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 之前调用它。

【讨论】:

  • 我认为这可能与文本字段中的输入有关?如果它在没有我的输入的 html 页面的情况下运行它,我假设是这样。当我输入值(将它们放在 url 中)时,页面似乎以某种方式刷新或更改
  • 啊,是的,onsubmit 方法会在您提交表单时刷新页面。将 onsubmit="calculateShirts(this)" 更改为 onsubmit="return calculateShirts(this)" 并添加 'return false;'到您的 calculateShirts 函数结束
  • 啊对,不敢相信我只是在看剧本..谢谢!
猜你喜欢
  • 2020-07-28
  • 1970-01-01
  • 2011-04-04
  • 2018-04-08
  • 1970-01-01
  • 2021-12-23
  • 1970-01-01
  • 2010-11-16
相关资源
最近更新 更多