admin管理员组

文章数量:1318156

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ".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("bo").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="bo">
      <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.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3/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("bo").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="bo">
      <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.

Share Improve this question asked May 26, 2013 at 19:05 giaotbgiaotb 6074 gold badges10 silver badges24 bronze badges 1
  • What is the problem? jsbin./oduqop/1 – Musa Commented May 26, 2013 at 19:08
Add a ment  | 

2 Answers 2

Reset to default 4

Try this

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

Inside your function:

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

Buon lavoro collega italiano ;)

本文标签: Retrieve combobox value in javascriptStack Overflow