简体   繁体   English

从select标签中获取选定的值

[英]Get selected value from select tag

When I select a value from the option and click on a button, I want to fetch the selected value with javascript. 当我从选项中选择一个值并单击一个按钮时,我想用javascript获取所选的值。 What am I doing wrong? 我究竟做错了什么? My value is always 1. 我的价值总是1。

 <select id="aand_select">
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                            <option value="4">4</option>
                            <option value="5">5</option>
    </select>

// javascript code // javascript代码

var e = document.getElementById("aand_select");
var quantity= e.options[e.selectedIndex].value;

This apparently will do 这显然会做到

var e = document.getElementById("aand_select");
e.addEventListener('change', function(){
  var quantity= e.options[e.selectedIndex].value;
  console.log(quantity);
},false);​​​​​​​​​​

Make sure that you are calling the javascript at the appropriate time. 确保您在适当的时候调用javascript。 For example if you are calling it only when the page loads, the value will never change. 例如,如果您仅在页面加载时调用它,则该值将永远不会更改。 Make sure you are calling it from an onClick() or some other event. 确保从onClick()或其他事件中调用它。

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

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