在javascript中的自定义选择中设置选项



html、css和js部分在这里的链接中。

问题:

这是一款电子桌面应用程序。

我想从数据库中检索一些数据并显示在窗口中。

如果不在gui中单击,我不知道如何设置自定义选择。

我需要从JavaScript本身设置选项。

感谢您的帮助。感谢

您需要以编程方式将选项添加到现有选择器中。考虑以下内容:

<select id="mySelector"></select>

假设你的电子应用程序模板中有上述内容。一旦您从数据库接收到数据,就动态地创建每个选项并附加到此选择器:

const data = [ { name: 'Option1', value: 'value1' }, { name: 'Option2', value: 'value2' } ]; // assuming the data is an array of objects
const sel = document.getElementById('mySelector');
data.forEach(opt => {
const option = document.createElement('option');
option.setAttribute('value', opt.value);
option.innerHTML = opt.name;
sel.appendChild(option);
});

生成的输出应如下所示:

<select id="mySelector">
<option value="value1">Option1</option>
<option value="value2">Option2</option>
</select>

希望这能帮助你了解如何使用JavaScript动态地向页面添加内容。

编辑

要选择一个选项,只需在选项中添加selected属性:

option.setAttribute('selected', true);

编辑2

在简单地看了一下您的代码(特别是js/assistance.js(之后,我可以看到您实际上正在创建一个div元素,并试图模仿option元素的特性。在这种情况下,设置selected属性将不起作用。由于您是自己创建这个div的,所以有很多方法可以将任何选项div标记为已选中。例如,您可以在每个选项div上侦听click事件,然后分配一个数据属性将其标记为选中。基于您的代码:

a.addEventListener('click', (e) => e.currentTarget.setAttribute('data-selected', 'true'));

上面的代码段假设a是使用document.createElement('div')创建的div元素。该代码段添加了一个click处理程序,单击后只需在选项上添加一个数据属性。现在,您应该添加其他检查,以确保从其他选项中删除data-selected属性,以防用户以后更改该选项。

最后,要查找所选选项,您可以尝试以下操作:

const selected = document.querySelector("div[data-selected='true']");

注意:data-selected的值是字符串,而不是布尔值。因此,您可以添加任何计算为true的值来指示选择。

根据我的理解,您需要进行选择操作,所以我在if selected语句中添加了所需的操作

if (elmnt == y[i]) {
arrNo.push(i)
document.getElementById("result").innerHTML = "You selected: " + y[i].innerHTML;
} else {
y[i].classList.remove("select-arrow-active");
}

检查代码段

var x, i, j, l, ll, selElmnt, a, b, c;
/*look for any elements with the class "custom-select":*/
x = document.getElementsByClassName("custom-select");
l = x.length;
for (i = 0; i < l; i++) {
selElmnt = x[i].getElementsByTagName("select")[0];
ll = selElmnt.length;
/*for each element, create a new DIV that will act as the selected item:*/
a = document.createElement("DIV");
a.setAttribute("class", "select-selected");
a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
x[i].appendChild(a);
/*for each element, create a new DIV that will contain the option list:*/
b = document.createElement("DIV");
b.setAttribute("class", "select-items select-hide");
for (j = 1; j < ll; j++) {
/*for each option in the original select element,
create a new DIV that will act as an option item:*/
c = document.createElement("DIV");
c.innerHTML = selElmnt.options[j].innerHTML;
c.addEventListener("click", function(e) {
/*when an item is clicked, update the original select box,
and the selected item:*/
var y, i, k, s, h, sl, yl;
s = this.parentNode.parentNode.getElementsByTagName("select")[0];
sl = s.length;
h = this.parentNode.previousSibling;
for (i = 0; i < sl; i++) {
if (s.options[i].innerHTML == this.innerHTML) {
s.selectedIndex = i;
h.innerHTML = this.innerHTML;
y = this.parentNode.getElementsByClassName("same-as-selected");
yl = y.length;
for (k = 0; k < yl; k++) {
y[k].removeAttribute("class");
}
this.setAttribute("class", "same-as-selected");
break;
}
}
h.click();
});
b.appendChild(c);
}
x[i].appendChild(b);
a.addEventListener("click", function(e) {
/*when the select box is clicked, close any other select boxes,
and open/close the current select box:*/
e.stopPropagation();
closeAllSelect(this);
this.nextSibling.classList.toggle("select-hide");
this.classList.toggle("select-arrow-active");
});
}
function closeAllSelect(elmnt) {
/*a function that will close all select boxes in the document,
except the current select box:*/
var x, y, i, xl, yl, arrNo = [];
x = document.getElementsByClassName("select-items");
y = document.getElementsByClassName("select-selected");
xl = x.length;
yl = y.length;
for (i = 0; i < yl; i++) {
if (elmnt == y[i]) {
arrNo.push(i)
document.getElementById("result").innerHTML = "You selected: " + y[i].innerHTML;
} else {
y[i].classList.remove("select-arrow-active");
}
}
for (i = 0; i < xl; i++) {
if (arrNo.indexOf(i)) {
x[i].classList.add("select-hide");
}
}
}
/*if the user clicks anywhere outside the select box,
then close all select boxes:*/
document.addEventListener("click", closeAllSelect);
/*the container must be positioned relative:*/
.custom-select {
position: relative;
font-family: Arial;
}
.custom-select select {
display: none; /*hide original SELECT element:*/
}
.select-selected {
background-color: DodgerBlue;
}
/*style the arrow inside the select element:*/
.select-selected:after {
position: absolute;
content: "";
top: 14px;
right: 10px;
width: 0;
height: 0;
border: 6px solid transparent;
border-color: #fff transparent transparent transparent;
}
/*point the arrow upwards when the select box is open (active):*/
.select-selected.select-arrow-active:after {
border-color: transparent transparent #fff transparent;
top: 7px;
}
/*style the items (options), including the selected item:*/
.select-items div,.select-selected {
color: #ffffff;
padding: 8px 16px;
border: 1px solid transparent;
border-color: transparent transparent rgba(0, 0, 0, 0.1) transparent;
cursor: pointer;
user-select: none;
}
/*style items (options):*/
.select-items {
position: absolute;
background-color: DodgerBlue;
top: 100%;
left: 0;
right: 0;
z-index: 99;
}
/*hide the items when the select box is closed:*/
.select-hide {
display: none;
}
.select-items div:hover, .same-as-selected {
background-color: rgba(0, 0, 0, 0.1);
}
<div class="custom-select" style="width:200px;">
<select id="selectone" onchange="selectfunc()">
<option value="0">Select car:</option>
<option value="1">Audi</option>
<option value="2">BMW</option>
<option value="3">Citroen</option>
<option value="4">Ford</option>
<option value="5">Honda</option>
<option value="6">Jaguar</option>
<option value="7">Land Rover</option>
<option value="8">Mercedes</option>
<option value="9">Mini</option>
<option value="10">Nissan</option>
<option value="11">Toyota</option>
<option value="12">Volvo</option>
</select>
</div>
<div id="result"></div>

最新更新