2010-05-10 88 views
0

我有一個頁面,允許用戶選擇表單中的一些東西,它會使用javascript計算重量。它將其分解成5個變量,我需要發送到另一個頁面。最初我只是把它放到一個文本框中,然後我發佈該文本框。但是我不想有5個文本框。所以現在我需要以某種方式發送或發佈五個變量到另一個頁面。這是我的javascript功能。我需要張貼weightBoxOne - weightBoxFive發送Javascript變量

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

剛剛成立了一個快速的JSON網絡服務和使用jQuery的崗位職能,或其他一些流行的Ajax框架的服務器上(jQuery是最好不過) – 2010-05-10 17:43:17

+0

...你不能使用數組而不是'weightBoxNineThousand'嗎? – kennytm 2010-05-10 17:43:19

+0

只有五個,但仍然不幫助我發佈 – shinjuo 2010-05-10 17:44:56

回答

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; 
+0

真棒我正在嘗試這個 – shinjuo 2010-05-10 17:50:30

+0

這很有用,非常感謝 – shinjuo 2010-05-10 19:21:34

0

忽略了一個事實,你可以破解這個有少量的變量工作,並假設你在服務器上同時控制客戶端Javascript和接收頁面,而不是不同的變量

  1. 使用數組什麼時候應該使用數組
  2. 數組序列化到使用JSON
  3. 後的表單字段串到你的接收頁面服務器上(隱藏或以其他方式)
  4. 反序列化的字符串JSON字符串到一個數組
  5. 使用它