我有以下代碼,我試圖添加「選擇」動態選項,當用戶選擇一個選項。我怎樣才能使用Javascript? 例如當用戶選擇 「糖果」 我想補充<option value="candy" selected>Candy</option>
如何添加「選擇」使用javascript選擇選項
function urlDirect() {
var businessTypeSelected = document.getElementById("BusinessType").value;
//alert("x " +x);
if (businessTypeSelected != "") {
window.location.href = location.host + businessTypeSelected;
document.getElementById("BusinessType").selectedIndex = document.getElementById("BusinessType").selectedIndex;
} else {
}
}
<span class="custom-dropdown custom-dropdown--blue custom-dropdown--large">
<select id="BusinessType" class="custom-dropdown__select custom-dropdown__select--blue" onChange="urlDirect()">
<option value="default">Select your business type</option>
<option value="auto">Auto </option>
<option value="aero">Aeroplane</option>
<option value="film">Film</option>
<option value="candy">Candy</option>
</select>
</span>
[可能的重複](http://stackoverflow.com/questions/4590311/set-option-selected-attribute-from-dynamic-created-option) – ODelibalta