日期:2014-05-16 浏览次数:20369 次
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>无标题页</title> <script type="text/javascript"> function sayValue(){ alert("值:" + document.getElementById('ddlTest').value); } </script> </head> <body> <div style="width:100%;text-align:center;" > <select id="ddlTest" onchange="alert('发生了onchange事件')" onpropertychange="alert('发生了onpropertychange事件')" > <option value="" ><--全部--></option> <option value="1" >湖南</option> <option value="2" >湖北</option> <option value="3" >广东</option> <option value="4" >广西</option> </select> <input id="Button2" type="button" value="改变下拉列表的值为广东" onclick="document.getElementById('ddlTest').value='3'" /> <input id="Button1" type="button" value="选中了:" onclick="sayValue()" /> </div> </body> </html>
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title>无标题页</title> </head> <body> <div style="width:100%; text-align:center;" > <select id="ddlTest"> <option value="0" >全部</option> <option value="1" >湖南</option> <option value="2" >湖北</option> <option value="3" >广东</option> <option value="4" >广西</option> </select> <input id="Button2" type="button" value="改变下拉列表的值为广东" /> <input id="Button1" type="button" value="选中了:" /> </div> <script> function $(id) { return document.getElementById(id); } function setValue(elem, value) { elem.value = value; if (elem.fireEvent) { elem.fireEvent("onchange"); } else { var evt = document.createEvent('HTMLEvents'); evt.initEvent('change', true, true); elem.dispatchEvent(evt); } } function sayValue() { alert("值:" + $('ddlTest').value); } $("Button2").onclick = function() { setValue($("ddlTest"), "3"); } $("Button1").onclick = sayValue; $("ddlTest").onchange = function() { alert('发生了onchange事件'); } </script> </body> </html>