簡體   English   中英

發送Javascript變量

[英]Sending Javascript variables

我有一個頁面,允許用戶選擇表單中的某些內容,它將使用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" />

通過將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;

忽略以下事實:您可以破解它以使用少量變量,並假設您同時控制客戶端Javascript和服務器上的接收頁面

  1. 應該使用數組時,使用數組而不是其他變量
  2. 使用JSON將數組序列化為字符串
  3. 將字符串以表單字段(隱藏或其他方式)發布到服務器上的接收頁面
  4. 將JSON字符串反序列化為數組
  5. 在服務器上使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM