javascript 在javascript中检索组合框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16762728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:53:49  来源:igfitidea点击:

Retrieve combobox value in javascript

javascriptcomboboxselectedvalue

提问by giacomotb

This is my little program, what could i do for take the combobox value??

这是我的小程序,我可以做什么来获取组合框值?

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <link rel="stylesheet" type="text/css" href="NewFile.css">
  <title>Insert title here</title>
  <script type="text/javascript">
    function calcola(){
        var op = String(document.getElementById("combo").value);
        alert(op);
    }   
  </script>
</head>
<body>
  <div class="in">
    Inserire il primo numero: 
    <input type="text" id="primo">
    <br>
    Inserire il secondo numero:
    <input type="text" id="secondo">
  </div>
  <div class="in">
    <select id="combo">
      <option selected="selected">*</option>
      <option>/</option>
      <option>+</option>
      <option>-</option>
    </select>
  </div>
  <div>
    <input type="button" name="bottone" value="Premi" onclick="calcola();">
  </div>

</body>
</html>

This program should be a calculator, but I omitted most of code because it's necessary to know the operator befor doing calculations.

这个程序应该是一个计算器,但我省略了大部分代码,因为在进行计算之前需要了解运算符。

回答by Harsha Venkatram

Try this

试试这个

function calcola()
{
    var element = document.getElementById("combo");
    var op = element.options[element.selectedIndex].value;
    alert(op);
} 

回答by Niccolò Campolungo

Inside your function:

在你的函数中:

function calcola(){
    var elt = document.getElementById("combo"),
        op = elt.options[elt.selectedIndex].text;
    alert(op);
}

Buon lavoro collega italiano ;)

Buon lavoro collega italiano ;)