发送Javascript变量

时间:2010-05-10 17:39:42

标签: javascript html forms post

我有一个页面,允许用户在表单中选择一些东西,它将使用javascript计算权重。它将它分解为我需要发送到另一个页面的5个变量。最初我只是把变量放到一个文本框中然后我发布了那个文本框。但是我不想要5个文本框。所以现在我需要以某种方式将五个变量发送或发布到另一个页面。这是我的javascript函数。我需要发帖weightBoxOne - weightBoxFive

js功能

function getWeight(){
      var weightBoxOne;
      var weightBoxTwo;
      var totalWeight;
      var box = .5;
   var quantity = document.dhform.quantity.value;
   var cardSize = document.dhform.cardSize.value;
   if(cardSize == 0.0141){
    if(quantity <= 1000){
     weightBoxOne = (quantity * cardSize) + box;
     totalWeight = weightBoxOne;
    }else if(quantity > 1000 && quantity <= 2000){
     weightBoxOne = (1000 * cardSize) + box;
     weightBoxTwo = ((quantity - 1000) * cardSize) + box;
     totalWeight = weightBoxOne + weightBoxTwo;
    }else if(quantity > 2000 && quantity <= 3000){
     weightBoxOne = (1000 * cardSize) + box;
     weightBoxTwo = (1000 * cardSize) + box;
     weightBoxThree = ((quantity - 2000) * cardSize) + box;
     totalWeight = weightBoxOne + weightBoxTwo + weightBoxThree; 
    }else if(quantity > 3000 && quantity <= 4000){
     weightBoxOne = (1000 * cardSize) + box;
     weightBoxTwo = (1000 * cardSize) + box;
     weightBoxThree = (1000 * cardSize) + box;
     weightBoxFour = ((quantity - 3000) * cardSize) + box;
     totalWeight = weightBoxOne + weightBoxTwo + weightBoxThree + weightBoxFour;
    }else{
     weightBoxOne = (1000 * cardSize) + box;
     weightBoxTwo = (1000 * cardSize) + box;
     weightBoxThree = (1000 * cardSize) + box;
     weightBoxFour = (1000 * cardSize) + box;
     weightBoxFive = ((quantity - 4000) * cardSize) + box;
     totalWeight = weightBoxOne + weightBoxTwo + weightBoxThree + weightBoxFour + weightBoxFive;
    }
   }else if(cardSize == 0.00949){
    if(quantity <= 4000){
     weightBoxOne = (quantity * cardSize) + box;
     totalWeight = weightBoxOne;
    }else{
     weightBoxOne = (4000 * cardSize) + box;
     weightBoxTwo = ((quantity - 4000) * cardSize) + box;
     totalWeight = weightBoxOne + weightBoxTwo;
    }
   }
   document.rates.weight.value = totalWeight;
  }
  //-->

这是最初发布的表单

<form action="getRates.php" name="rates" method="post" onSubmit="popupform(this, 'join')">
                          <table style="width: 216px">
                            <tr>
                              <td style="width: 115px; height: 49px;"><span class="style16">Weight</span><br/>
                                  <input type="text" id="weight" name="weight" size="10" maxlength="4"/>
                              </td>
                              <td align="right" style="width: 68px; height: 49px;" valign="top"><span class="style16">Zip Code</span><br/>
                                  <input type="text" id="zip" name="zip" size="10" maxlength="5"/>
                              </td>
                            </tr>
                            <tr>
                              <td style="width: 115px">
         <input name="submit" type="submit" value="Get Rate Costs" style="width: 138px" />

2 个答案:

答案 0 :(得分:1)

我通过将JavaScript变量传递到隐藏字段并像往常一样在表单中发布来完成类似的事情。

只需在表单中添加一些隐藏字段,其中包含以下标记:

<input type="hidden" id="hidden-field-1" value="">

然后,您可以调整隐藏字段的值以匹配您的JavaScript变量,并将值传递到下一页:

document.rates.hidden-field-1.value = totalWeight;

请记住,这种形式的JavaScript(我直接从你的帖子中获取)只能在Internet Explorer中可靠地运行...更好的方法是使用:

document.forms['rates'].hidden-field-1.value = totalWeight;

答案 1 :(得分:0)

忽略这样一个事实,即您可能会破解这个以处理少量变量,并假设您同时控制客户端Javascript和服务器上的接收页面

  1. 使用数组时使用数组而不是使用不同的变量
  2. 使用JSON
  3. 将数组序列化为字符串
  4. 在表单字段(隐藏或其他方式)中将字符串发布到服务器上的接收页面
  5. 将JSON字符串反序列化为数组
  6. 在服务器上使用它