简体   繁体   English

如何更新占位符<select>选择选项 - Vanilla JS

[英]How to update placeholder of <select> with selected option - Vanilla JS

I want the selected option to show as the placeholder when the user selects one.当用户选择一个时,我希望选定的选项显示为占位符。

Currently, it shows nothing, not even the option with selected : shows nothing目前,它什么也不显示,甚至没有selected的选项:不显示

I am updating the options with vanilla js..我正在使用 vanilla js 更新选项。

let select = document.querySelector("#category");

let items = ["one", "two", "three"];
let str = ""
for (let item of items) {
  str += `<option value=${item}>` + item + "</option>";
}
select.innerHTML = "<option disabled selected>Select a category</option>" + str;

So, whatever option is selected should populate the placeholder of the select box.因此,无论选择什么选项都应该填充选择框的占位符。

Never mind..没关系..

There was too much padding on the element, which was causing it not to show.元素上的填充过多,导致它不显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM