2012-02-20 63 views
-1

我有一個帶有8個收音機輸入按鈕的表格,我想根據用戶的收音機輸入選擇更新總價格。如何通過jQuery實現這一點?基於收音機輸入選擇更新總價

<table> 
    <tr> 
    <td></td> 
    <form name="m" action="" method="post"> 
     <th> <input id="test" name="purchaseChoice" class="radio" onclick="togglePrices(false);" type="radio" value="P2"/> 
     <label for="purchaseChoice">£5</span> </label> 
     <input id="test" name="purchaseChoice" class="radio" onclick="togglePrices(true);" type="radio" value="P1"/> 
     <label for="purchaseChoice">£10</label> 
     </th> 
     <th class=""> <input id="test" name="purchaseChoice" class="radio" onclick="togglePrices(true);" type="radio" value="S111"/> 
     <label for="purchaseChoice">£20</label> 
     <input id="test" name="purchaseChoice" class="radio" onclick="togglePrices(false);" type="radio" value="S112"/> 
     <label for="purchaseChoice">£30</label> 
     </th> 
     <th class=""> <input id="test" name="purchaseChoice" class="radio" onclick="togglePrices(true);" type="radio" value="S131"/> 
     <label for="purchaseChoice">£40</label> 
     <input id="test" name="purchaseChoice" class="radio" onclick="togglePrices(false);" type="radio" value="S132"/> 
     <label for="purchaseChoice">£50</label> 
     </th> 
     <th class="highlight"> <input id="test" name="purchaseChoice" class="radio" onclick="togglePrices(true);" type="radio" value="S121"/> 
     <label for="purchaseChoice">£60</label> 
     <input id="test" name="purchaseChoice" class="radio" onclick="togglePrices(false);" type="radio" value="S122"/> 
     <label for="purchaseChoice">£70</label> 
     </th> 
    </form> 
    </tr> 
    <tr> 
    <th class="total">TOTAL YOU PAY</th> 
    <th> 
    <div class="Priceband"></div> 
    </th> 
    </tr> 
</table> 

感謝

+0

您的價格值在哪裏? – 2012-02-20 11:36:59

+0

價格值出現在輸入字段末尾。基本上,正如你可以看到我有8個值:P1,P2,S112,S111,S121,S122,S131和S132 – gek 2012-02-20 11:54:47

回答

0

如果我正確地理解你的要求比你只是想更新價格與選擇更改。

請找到下面的代碼。可能對你有所幫助。

<table> 
    <tr> 
    <td></td> 
    <form name="m" action="" method="post"> 
     <th> <input id="test" name="purchaseChoice" class="radio" onclick="togglePrices(this);" type="radio" value="P2"/> 
     <label for="purchaseChoice">£5</span> </label> 
     <input id="test" name="purchaseChoice" class="radio" onclick="togglePrices(this);" type="radio" value="P1"/> 
     <label for="purchaseChoice">£10</label> 
     </th> 
     <th class=""> <input id="test" name="purchaseChoice" class="radio" onclick="togglePrices(this);" type="radio" value="S111"/> 
     <label for="purchaseChoice">£20</label> 
     <input id="test" name="purchaseChoice" class="radio" onclick="togglePrices(this);" type="radio" value="S112"/> 
     <label for="purchaseChoice">£30</label> 
     </th> 
     <th class=""> <input id="test" name="purchaseChoice" class="radio" onclick="togglePrices(this);" type="radio" value="S131"/> 
     <label for="purchaseChoice">£40</label> 
     <input id="test" name="purchaseChoice" class="radio" onclick="togglePrices(this);" type="radio" value="S132"/> 
     <label for="purchaseChoice">£50</label> 
     </th> 
     <th class="highlight"> <input id="test" name="purchaseChoice" class="radio" onclick="return togglePrices(this);" type="radio" value="S121"/> 
     <label for="purchaseChoice">£60</label> 
     <input id="test" name="purchaseChoice" class="radio" onclick="togglePrices(this);" type="radio" value="S122"/> 
     <label for="purchaseChoice">£70</label> 
     </th> 
    </form> 
    </tr> 
    <tr> 
    <th class="total">TOTAL YOU PAY</th> 
    <th> 
    <div class="Priceband"></div> 
    </th> 
    </tr> 
</table> 
<script language="javascript" type="text/javascript" > 

    function togglePrices(e) { 
     var price = $(e).val(); 
     $(".Priceband").html(price); 
    } 

</script> 
+0

我試過,但我不能得到任何價格顯示在我的「Priceband」div – gek 2012-02-20 17:27:34

+0

我試過在我的結尾相同的代碼。它爲我工作很好。可能你缺少Jquery引用。確保有jQuery庫的參考。 – 2012-02-21 04:43:23

+0

我確定jQuery引用,因爲頁面上的其他jQuery代碼工作正常。我已經更新了上面的代碼,請您再次查看。我從第一次粘貼時發現了一些錯誤,也許這就是爲什麼不工作?非常感謝您的幫助,迄今爲止 – gek 2012-02-21 16:58:20