我有以下代码,我试图添加“选择”动态选项,当用户选择一个选项。我怎样才能使用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