2013-10-20 50 views
0

我喜歡靜態的HTML網站把 兩個下拉選擇菜單,並根據上選擇要顯示在一個DIV結果(聯繫方式)顯示選擇後一種選擇到一個DIV兩個下拉

這裏是什麼在做,但對我錯了

這裏是JavaScript的

function setOptions(chosen) { 
var selbox = document.myform.opttwo; 

selbox.options.length = 0; 
if (chosen == " ") { 
    selbox.options[selbox.options.length] = new Option('Please select one of the options above first', ' '); 

} 
if (chosen == "1") { 
    selbox.options[selbox.options.length] = new 
    Option('first choice - option one', 'oneone'); 
    selbox.options[selbox.options.length] = new 
    Option('first choice - option two', 'onetwo'); 
} 
if (chosen == "2") { 
    selbox.options[selbox.options.length] = new 
    Option('second choice - option one', 'twoone'); 
    selbox.options[selbox.options.length] = new 
    Option('second choice - option two', 'twotwo'); 
} 
if (chosen == "3") { 
    selbox.options[selbox.options.length] = new 
    Option('third choice - option one', 'threeone'); 
    selbox.options[selbox.options.length] = new 
    Option('third choice - option two', 'threetwo'); 
} 

}

,這裏是HTML

<form name="myform"> 
<div align="center"> 
    <select name="optone" size="1" onchange="setOptions(document.myform.optone.options [document.myform.optone.selectedIndex].value);"> 
     <option value="0" selected="selected"></option> 
     <option value="1">First Choice</option> 
     <option value="2">Second Choice</option> 
     <option value="3">Third Choice</option> 
    </select> 
    <br> 
    <br> 
    <select name="opttwo" size="1"> 
     <option value="0" selected="selected">Please select one of thed options above first</option> 
     <option value="www.google.com" selected="selected">www.google.com</option> 
     <option value="www.facebook.com" selected="selected">facebook.com</option> 
    </select> 
    <input type="button" name="go" value="Value Selected" onclick="window.open(document.myform.opttwo.options[document.myform.opttwo.selectedIndex].value);"> 
</div> 

http://jsfiddle.net/HZNJa/

這裏是我喜歡它提前

http://d.pr/i/BjjR

感謝

+0

難道答案如下解決? –

回答

0

你給setOptions從HTML調用返回以下錯誤:未捕獲的ReferenceError:未定義setOptions。

這裏的解決方案:http://jsfiddle.net/HZNJa/6/

給你選擇一個ID,刪除內聯調用給setOptions,然後調用#id.onchange事件處理程序在你的JS那裏引用setOptions:

document.getElementById('changer').onchange = function() { 
    setOptions(document.myform.optone.options[document.myform.optone.selectedIndex].value); 
} 
相關問題