我只是想从下拉菜单中检索所选选项的值。我整理了一些杂乱无章的东西,几乎可以正常工作了。
什么是适合此任务的解决方案?
您会看到从下拉菜单中有四个选项值可供选择:奥迪,雪铁龙,宝马,特斯拉。我想知道从下拉列表中选择了哪个值,以便在与选择相对应的页面上显示内容。
代码如下:
// WHAT IS THE BEST WAY TO RETRIEVE THE VALUE FROM THE DROPDOWN MENU?
function pageSelected() {
if (document.getElementById("myDropdown").value == 1) {
document.getElementById("optionvalue").innerHTML = "Audi";
} else if (document.getElementById("myDropdown").value == 2) {
document.getElementById("optionvalue").innerHTML = "Citroen";
} else if (document.getElementById("myDropdown").value == 3) {
document.getElementById("optionvalue").innerHTML = "BMW";
} else if (document.getElementById("myDropdown").value == 4) {
document.getElementById("optionvalue").innerHTML = "Ford";
}
}
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function ddm() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #2980B9;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #ddd;}
.show {display: block;}
<div id="infobar">
<div id="info">
</div>
<!--dropdown option value goes here-->
<div id="optionvalue">option value</div>
<div class="dropdown">
<button onclick="ddm()" class="dropbtn">▼</button>
<div id="myDropdown" class="dropdown-content">
<a><option value="1" onclick="pageSelected()" >Audi</option></a>
<a><option value="2" onclick="pageSelected()">Citroen</option></a>
<a><option value="3" onclick="pageSelected()">BMW</option></a>
<a><option value="4" onclick="pageSelected()">Ford</option></a>
</div>
</div>
最佳答案
您可以将元素传递到pageSelected
并从那里获取值
<option value="1" onclick="pageSelected(this)" >Audi</option>
function pageSelected(option) {
document.getElementById("optionvalue").innerHTML = option.value + ' ' + option.innerHTML;
}
// WHAT IS THE BEST WAY TO RETRIEVE THE VALUE FROM THE DROPDOWN MENU?
function pageSelected(option) {
document.getElementById("optionvalue").innerHTML = option.value + ' ' + option.innerHTML;
}
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function ddm() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #2980B9;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #ddd;}
.show {display: block;}
<div id="infobar">
<div id="info">
</div>
<!--dropdown option value goes here-->
<div id="optionvalue">option value</div>
<div class="dropdown">
<button onclick="ddm()" class="dropbtn">▼</button>
<div id="myDropdown" class="dropdown-content">
<a><option value="1" onclick="pageSelected(this)" >Audi</option></a>
<a><option value="2" onclick="pageSelected(this)">Citroen</option></a>
<a><option value="3" onclick="pageSelected(this)">BMW</option></a>
<a><option value="4" onclick="pageSelected(this)">Ford</option></a>
</div>
</div>
关于javascript - JavaScript:从下拉菜单中检索所选值的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53502774/