fbt045 发表于 2012-12-17 16:23

请问怎样自动调用onchange事件啊?

各位高手,我现在有个select下拉选项,希望不用手动点击内容改变就能自动执行一次onchage事件的函数,请问该怎样实现呢?

fbt045 发表于 2012-12-18 10:52

我的想法是手动触发一个onchange事件,而不是手动选择,我在网上找到下面方法:
   <html><body>
   
    <select id="sel" name="test" onchange="demo()">
      <option value="1" selected>测试一</option>
  <option value="2">测试二</option>
  <option value="3">测试三</option>
  <option value="4">测试四</option>
   </select>
 <input id="tex" type="text" name="text1"id="text1">
   
<script>

document.getElementById("sel").value="3";
document.getElementById("sel").fireEvent("onchange");
function demo()
{
   var d=document.getElementById("sel").value;
   document.getElementById("tex").value=d;
   //alert(d);

}
</script>
    </body></html>
但试了一下没有效果,不知道大家有什么想法,谢谢哈

satrun7 发表于 2012-12-18 22:03

<!DOCYTPE html>
<html>
<body>
<select id="sel" name="test" onChange="demo()">
                <option value="1" selected>测试一</option>
  <option value="2">测试二</option>
  <option value="3">测试三</option>
  <option value="4">测试四</option>
</select>
<input id="tex" type="text" name="text1"id="text1">

<script text="text/javascript">
//document.getElementById("sel").value="3";
//document.getElementById("sel").fireEvent("onchange");
function demo()
{
        var d=document.getElementById("sel").value;
        document.getElementById("tex").value=d;
        alert(d);
}
//网页加载完成触发demo
window.onLoad = demo();
</script>
</body>
</html>

maochanglu 发表于 2012-12-19 10:51

去学习jquery吧。 原生的js看起来累
页: [1]
查看完整版本: 请问怎样自动调用onchange事件啊?