HTML
<h4>What carrier do you have?</h4>
<select style="margin-top: 1pt" id="carrier">
<option value="">Choose your carrier...</option>
<option value="ATT">AT&T</option>
<option value="Other">Other</option>
<option value="Unlocked">Unlocked</option>
</select>
<h4>What is your phones capicity?</h4>
<select style="margin-top: 1pt" id="capacity">
<option value="">Choose your capacity...</option>
<option value="8">8GB</option>
<option value="16">16GB</option>
</select>
<h4>What color is your phone?</h4>
<select style="margin-top: 1pt" id="color">
<option value="">Choose your color...</option>
<option value="black">Black</option>
<option value="white">White</option>
</select>
<p>
<input disabled id="mybutton" type="button" value="Next"/>
</p>
Javascript
将窗格中的第二个下拉菜单从 更改onLoad
为No wrap - in <head>
。我知道我没有使用完美的加载事件,但这目前有效。:p 所有 URL 都存储在 JSON 对象中,因此您可以更轻松地对其进行编辑。现在它只是用选定的 URL 发出警报。
var URLs = {
'ATT': {
'8': {
'black': 'http://www.google.com/',
'white': 'http://www.yahoo.com/'
},
'16': {
'black': 'http://www.dogpile.com/',
'white': 'http://www.bing.com/'
}
},
'Other': {
'8': {
'black': 'http://www.google.com',
'white': 'http://www.yahoo.com'
},
'16': {
'black': 'http://www.dogpile.com/',
'white': 'http://www.bing.com/'
}
},
'Unlocked': {
'8': {
'black': 'http://www.google.com',
'white': 'http://www.yahoo.com'
},
'16': {
'black': 'http://www.dogpile.com/',
'white': 'http://www.bing.com/'
}
}
};
// check to see if all selects have a value that isn't the first one
function checkInput() {
var disable = false;
var select = document.getElementsByTagName('select');
for(var i=0,l=select.length;i<l;i++) {
if(select[i].selectedIndex==0)
{ disable = true; }
}
document.getElementById('mybutton').disabled = disable;
}
function go() {
var carrierE = document.getElementById('carrier');
var carrier = carrierE.getElementsByTagName('option')[carrierE.selectedIndex].value;
var capacityE = document.getElementById('capacity');
var capacity = capacityE.getElementsByTagName('option')[capacityE.selectedIndex].value;
var colorE = document.getElementById('color');
var color = colorE.getElementsByTagName('option')[colorE.selectedIndex].value;
window.alert(URLs[carrier][capacity][color]);
}
function init() {
var select = document.getElementsByTagName('select');
// attach checkInput to all select elements
for(var i=0,l=select.length;i<l;i++) {
if(select[i].addEventListener)
{ select[i].addEventListener('change',checkInput,false); }
else if(select[i].attachEvent)
{ select[i].attachEvent('onchange',checkInput); }
else
{ select[i].onchange = checkInput; }
}
var button = document.getElementById('mybutton');
if(button.addEventListener)
{ button.addEventListener('click',go,false); }
else if(button.attachEvent)
{ button.attachEvent('onclick',go); }
else
{ button.onclick = go; }
}
if(document.addEventListener) {
document.addEventListener('DOMContentLoaded',init,false);
}
else {
// I can do better but it's not important here
window.onload = init;
}