【问题标题】:Sending Javascript variables发送 Javascript 变量
【发布时间】:2010-05-10 17:39:41
【问题描述】:

我有一个页面,允许用户在表单中选择一些东西,它会使用 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" />

【问题讨论】:

  • 只需设置一个快速的 json web 服务并使用 jquery 的 post 功能,或其他一些流行的 ajax 框架(虽然 jquery 是最好的)
  • ...你不能用数组代替weightBoxNineThousand吗?
  • 只有五个,但仍然无法帮助我发布它
  • 我对json web servie不熟悉

标签: javascript html forms post


【解决方案1】:

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

只需在表单中添加一些隐藏字段,使用如下标签:

&lt;input type="hidden" id="hidden-field-1" value=""&gt;

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

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

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

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

【讨论】:

    【解决方案2】:

    忽略您可以破解它以使用少量变量这一事实,并假设您同时控制客户端 Javascript 和服务器上的接收页面

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      相关资源
      最近更新 更多