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對象中,因此您可以更輕鬆地進行編輯。現在它只是提醒您選擇的網址。
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;
}
在我看來,他們都沒有工作? – tomca32
''onClick'在選項中贏得''而不是'onChange'中的'onChange'在'
有很多問題.... 1)'tryToMakeLink'是負載回調中的私有方法,所以它在全局中不可用上下文。 2)'選項'元素onclick不被廣泛支持使用'select's'onchange'來代替'3)':selected'不是一個有效的CSS選擇器......看起來你試圖將jQuery選擇器與本地javascript混合使用 –