javascript 中select标签上放入onchange事件的两种方式分享


摘要:
下文讲述在html标签select上放入onchange事件的方式分享,如下所示:

例1:

 <meta charset='utf-8' />
<p>
 maomao365.com<br />
js select上直接使用onchange的方法
</p>
<hr /> 
<script>
 function showDiv(divInfo)
 {
	 var d = document.getElementById(divInfo);
     d.style.display ="block";   
 }

</script>  
<select id="transfer_reason" name="transfer_reason"
 onChange="javascript:showDiv('other');">
<option value="1">选项--1</option>
<option value="2">选项--2</option>
<option value="3">选项--3</option>
</select>
<div id="other" style="display: none;">div默认被隐藏</div>

例2:

<meta charset='utf-8' />
<p>
 maomao365.com<br />
js select绑定onchange方法
</p>
<hr /> 
<script>
 function showDiv(divInfo)
 {
	 var d = document.getElementById(divInfo);
     d.style.display ="block";
 }

 window.onload = function() {
        var eSelect = document.getElementById('a'); 
        eSelect.onchange = function() { 
             showDiv('other');        
        }
    }
</script>  

<select id="a">
<option value="1">选项--1</option>
<option value="2">选项--2</option>
<option value="3">选项--3</option>
</select>
 
<div id="other" style="display:none;">div默认被隐藏</div>