Javacsript:从下拉菜单中检索选定值的最佳方法是什么?

时间:2018-11-27 15:20:02

标签: javascript html drop-down-menu

我只是想从下拉菜单中检索所选选项的值。我整理出一些杂乱无章的东西,几乎可以正常工作。

什么是适合此任务的解决方案?

您会看到从下拉菜单中有四个选项值可供选择:奥迪,雪铁龙,宝马,特斯拉。我想知道从下拉列表中选择了哪个值,以便在与选择相对应的页面上显示内容。

以下代码:

// 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">&#9660;</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>

4 个答案:

答案 0 :(得分:2)

您可以将元素传递到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">&#9660;</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>

答案 1 :(得分:0)

从Google的第一个结果改编为“ javascript get dropdown value”:

var e = document.getElementById ("myDropdown");
var selectedValue = e.options[e.selectedIndex].value;

答案 2 :(得分:0)

取决于您的追求。 如果您需要该值(ID),则可以使用以下代码:

$("#ID of drop").val()

但是,如果您在文字后面,我建议使用此文字:

$("#ID of drop option:selected").text()

答案 3 :(得分:0)

您可能对HTML <select> tag for your dropdown感兴趣。这将与浏览器配合使用,you can also attach an event listener会在与下拉列表进行交互时触发。

<p>Selected: <span id="value"></span></p>

<select id="dropdown">
  <option value="0">Audi</option>
    <option value="1">Citroen</option>
    <option value="2">BMW</option>
    <option value="3">Tesla</option>
</select>

然后

var dropdown = document.getElementById("dropdown");

// When the dropdown is changed,
// this function runs
dropdown.addEventListener("change", handleSelect);

// Obtain the default value
var selected = dropdown.value;
var displayValue = document.getElementById("value");

displayValue.innerText = selected;

// Event handler ran when the
// dropdown changes
function handleSelect(event) {
  var updatedValue = event.target.value;
  displayValue.innerText = updatedValue;
}