2010-09-15 44 views
0

我得到這個代碼放在這裏最近幫助用JavaScript來隱藏單選按鈕

<script type='text/javascript'> 
$('#discountselection').hide(); 

    $('#No').click(function(){ 
     $('#discountselection').hide(); 
    }); 

    $('#Yes').click(function(){ 
     $('#discountselection').show(); 
    }); 
</script> 

其目的是隱藏一個下拉框,這取決於是否單選按鈕yes和no選擇。這裏是我的代碼:

<td width="338">Would You like to avail of our multiyear discounts?* 
    <br />See <a href="Pricing">Pricing</a> for more details 
</td> 
<td colspan="4"> 
    <input name="discount" type="radio" id="Yes" value="Yes" />Yes 
    <input name="discount" type="radio" id="No" value="No" checked="checked" />No<br /> 
    <select class="purple" name="discountselection" id="discountselection"> 
     <option value="1" selected="selected">1 Year</option> 
     <option value="2">2 Years</option> 
     <option value="3">3 Years</option> 
     </select>     
</td> 

我的頭標籤之間放置在JavaScript中,但由於某種原因,這恰恰是不是爲我工作。我根本不熟悉JavaScript。如果有人能夠看到我出錯的地方,這將是一個很大的幫助。 謝謝。

+0

您是否包含jQuery?這個腳本似乎使用它。 – halfdan 2010-09-15 08:59:46

+0

「之間的標籤」是什麼意思?什麼標籤? – annakata 2010-09-15 09:03:45

+0

@ halfdan ..我以爲我的問題已經解決,因爲我忘記了鏈接到jquery文件,但後來我做到了這一點但是它仍然沒有工作。 @annakata - 抱歉,我的意思是標題標籤,由於某種原因,頭部沒有顯示。 – 109221793 2010-09-15 09:05:15

回答

2

你的代碼很好,你只需要用ready處理程序包裝它。

這樣,

$(function() { 
    $('#discountselection').hide(); 

    $('#No').click(function() { 
     $('#discountselection').hide(); 
    }); 

    $('#Yes').click(function() { 
     $('#discountselection').show(); 
    }); 
});​ 

你也可以縮短你的代碼是這樣,

$(function() { 
    $('#discountselection').hide(); 
    $('#Yes, #No').click(function() { 
     $('#discountselection').toggle(this.value==='Yes'); 
    }); 
});​ 
+1

謝謝,這麼簡單但很重要! – 109221793 2010-09-15 09:11:30

+0

沒問題...看到加入的指針;) – Reigel 2010-09-15 09:29:10

0

該代碼適用於我。

如果它不爲你工作,嘗試了幾件事情:

  • 確保jQuery是適當地包括和工作
  • 把你的事件處理代碼的任何地方標記與後那些元素
+0

嗨德蘭,我認爲你完全複製了我的代碼?我只是不明白它會是什麼。這個代碼甚至可以用在jsfiddle上,但它似乎並沒有爲我所用。 – 109221793 2010-09-15 09:08:34

+0

應該是一個評論...你可能想在任何向下投票之前刪除這個;) – Reigel 2010-09-15 09:09:04

+0

完成,我已經添加了一些確定的提示。 – 2010-09-15 09:10:28

1

如果您不需要jQuery的,這裏是做

<script type="text/javascript"> 
function showDiscount(show) { 
    document.getElementById('discountselection').style.display=(show)?'block':'none'; 
} 
window.onload=function() { 
    showDiscount(document.getElementById('discountYes').checked); 
} 
</script> 
<table> 
<tr> 
<td width="338">Would You like to avail of our multiyear discounts?* 
    <br />See <a href="Pricing">Pricing</a> for more details 
</td> 
<td colspan="4"> 
    <input name="discount" type="radio" id="discountYes" value="Yes" onclick="showDiscount(this.checked)"/>Yes 
    <input name="discount" type="radio" id="discountNo" value="No" checked="checked" onclick="showDiscount(!this.checked)"/>No<br /> 
    <select class="purple" name="discountselection" id="discountselection" style="display:none"> 
     <option value="1" selected="selected">1 Year</option> 
     <option value="2">2 Years</option> 
     <option value="3">3 Years</option> 
     </select>     
</td> 
</tr> 
</table> 
0
的一種方式

您使用的$()語法是jQuery語法,而不是純Javascript。 JQuery是一個Javascript庫,它增加了普通JS的附加功能。如果你使用這個語法,你需要包含JQuery庫。

即使沒有JQuery,你試圖實現的東西也相對簡單,但如果你不打算使用JQuery,代碼看起來會完全不同。

如果您打算使用JQuery,則需要將代碼包裝在docuemnt就緒區塊中,以便它能夠正確初始化。