如何在javascript中选择列表框选项时执行脚本?

时间:2013-10-15 01:18:21

标签: javascript listbox

当用户从我的列表框中选择一个选项时,我需要知道如何执行脚本。我目前的代码(虽然这可能是错误的方法是:

<form>
    Character Name: <input type="text" name="charname">
</form>
    Server Region: <select id="serverloc">
        <option value="4">US</option>
        <option value="Oceanic">Oceanic</option>
    </select>

<script>
   var serverloc = document.getElementById(4);
   if ( serverloc == "Oceanic" ) {
      document.Write("it Worked!")
   }
</script>

1 个答案:

答案 0 :(得分:1)

Server Region: <select id="serverloc">
    <option value="4">US</option>
    <option value="Oceanic">Oceanic</option>
</select>

JS:

var select = document.getElementById('serverloc');
select.onchange = function(){ alert('yay'); };