2013-10-20 21 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); 
} 
相关问题