2015-12-28 187 views
-2
<html> 

<head> 
</head> 
<script> 
    function ClickFun() { 
     document.getElementById("print").innerHTML = 100; 
    } 

    function ClickFunction() { 
     document.getElementById("print").innerHTML = 200; 
    } 

    function Click() { 
     document.getElementById("print").innerHTML = 150; 
    } 

    function hello() { 
     alert("you clicked the list"); 
    } 
</script> 

<body> 
    <select> 
     <option onclick="ClickFunction();">Chrome</option> 
     <option onclick="Click();">Mozilla</option> 
     <option onclick="ClickFun(); hello();">Eclipse</option> 
    </select> 
    <p id="print"></p> 
</body> 

</html> 
+1

的可能的複製[jQuery的選擇選項單擊處理(http://stackoverflow.com/questions/5749597/jquery-select-option-click-handler) –

+1

不要使用單擊選擇選項的事件,使用更改事件。 –

回答

0

正如Alexander所建議的那樣,在select選項上使用onchange事件。

<html> 

<head> 
</head> 
<script> 
    function print(){ 
    switch(document.getElementById("browser").value){ 
     case "c": 
     document.getElementById("print").innerHTML = 100; 
     break; 
     case "m": 
     document.getElementById("print").innerHTML = 200; 
     break; 
     case "e": 
     document.getElementById("print").innerHTML = 150; 
     default: 
     alert("you clicked the list"); 
    } 
    }; 
</script> 

<body> 
    <select id="browser" onchange="print()"> 
     <option value="c">Chrome</option> 
     <option value="m">Mozilla</option> 
     <option value="e">Eclipse</option> 
    </select> 
    <p id="print"></p> 
</body> 

</html>