2016-03-05 124 views
0

我正在嘗試一段時間,現在在這裏和其他網站上到處搜索。基於選定的單選按鈕添加/刪除下拉選項

我找到某種解決方案在這裏http://jsfiddle.net/zszqs/

的,但是當我嘗試在我的代碼,我的實際網頁上顯示,這是行不通的。它在示例網站tho中,如果我將所有代碼更改爲我的。

<input name="documenttype" type="radio" required id="dType1" value="Pro-Forma">Pro-Forma 
<input name="documenttype" type="radio" required id="dType2" value="Invoice">Invoice 

<select name="paymentmethod" required id="paymentmethod"> 
<option value="" default selected>Select...</option> 
<option value="TT">Online Bank Transfer</option> 
<option value="PP">PayPal</option> 
<option value="CC">Credit Card</option> 
</select> 

基本上我想要刪除的信用卡選項時,無線電備考選項被選中,或者如果用戶改變無線電發票回來把它...

<script> 
var option = '<option value="CC">Credit Card</option>'; 
$('input[name=documenttype]').change(function() { 
    var $select = $("#paymentmethod"); 
    if (this.id == 'dType1') { 
     $select.find("option[value='CC']").remove() 
    } else if (!$select.find("option[value='CC']").length) { 
     $select.append(option) 
    } 
}) 
</script> 

我覺得我不是在頁面加載時收取代碼,或者類似的東西。 我對jquery完全陌生,所以我可能錯過了,我不知道,在哪裏把<script>(頭或身體?),或者如果我必須指定一些onLoad選項。

+1

告訴我們您的代碼,請 – Banana

+0

我的代碼是你看... ...在頂部鏈接示例代碼的一個是我想達到 – Stefano

+0

我只看到了一半的東西。如果問題出現在javascript部分,我希望看到它。同時,確保你1) - 鏈接jquery庫作爲小提琴中的代碼在jquery中。 2)將代碼塊放在所有元素後面的最後,或者在onLoad函數的最後部分 – Banana

回答

1

與jquery腳本代碼

$(document).ready(function() 
      { 
       $(".documenttype").click(function() 
       { 
         $("option[value='CC']").css("display", $('#dType1').is(":checked") ? "none" : "block"); 

       }); 

});

HTML代碼

<input class="documenttype" name="documenttype" type="radio" required id="dType1" value="Pro-Forma">Pro-Forma 
      <input class="documenttype" name="documenttype" type="radio" required id="dType2" value="Invoice">Invoice** 
      <select name="paymentmethod" required id="paymentmethod"> 
       <option value="" default selected>Select...</option> 
       <option value="TT">Online Bank Transfer</option> 
       <option value="PP">PayPal</option> 
       <option value="CC">Credit Card</option> 
      </select> 
相關問題