0

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

<!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>

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

4

2 回答 2

4

尝试这个

function calcola()
{
    var element = document.getElementById("combo");
    var op = element.options[element.selectedIndex].value;
    alert(op);
} 
于 2013-05-26T19:09:19.427 回答
0

在你的函数里面:

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

Buon lavoro collega italiano ;)

于 2013-05-26T19:09:11.650 回答