我有多个选项的选择标签,当加载页面时,我想摆在第一位置选择的选项列表。我使用的角度,所以我的选择代码是:
<select class="form-control" id="field_shopName" multiple name="shopName [(ngModel)]=" product.shopNames ">
<option [ngValue]="getSelected(product.shopNames, shopPdvOption) " *ngFor="let shopPdvOption of shoppdvs; trackBy: trackShopPdvById ">{{shopPdvOption.shopPdV}}</option>
</select>
是否有可能只用HTML5或JavaScript来实现这一目标?
有可能的。下面的例子将先加水果的一个额外的列表。
window.onload = function () {
var select = document.getElementById("fruits");
var firstFruits = ['Kiwi', 'Peach', 'Orange'].reverse();
firstFruits.forEach(function (fruit) {
var option = document.createElement('option');
option.text = option.value = fruit;
select.add(option, 0);
})
};
<select id="fruits">
<option>Apple</option>
</select>