2011-07-05 52 views
3

我有一個選擇表單並帶有幾個選項。我也有一個div,我想只在選擇特定選項時才顯示。你們能否指出我正確的方向?最簡單的事情是什麼?如何在用戶進行選擇時切換可見性

謝謝:)

+0

http://stackoverflow.com/questions/835259/show-hide -fields-depening-select-value –

回答

2

下面是一個簡單的和非jQuery的解決方案

window.onload=function() { 
    var sel = document.getElementById("sel1"); 
    sel.onchange=function() { 
    document.getElementById("otherDiv").style.display=(this.value=="other")?"":"none"; 
    } 
    sel.onchange(); // set the visibility onload too in case of reload 
} 

<select id="sel1"> 
. 
. 
. 
<option value="other">Other</option> 
</select> 
<div id="otherDiv">Other options</div> 
+0

謝謝我實際上最終使用了這個。有時jQuery可能有點複雜! – bdeonovic

+0

我開始越來越喜歡jQuery,但它不應該是一切的下跪解決方案 - 尤其是不是一個簡單的頁面,它不是已經使用jQuery。在這裏,你可能會得到一個建議,在幾秒鐘內使用jQuery來詢問如何添加a和b;) – mplungjan