<script language="javascript" type="text/javascript">
function change(){
alert("dd");
}
function clk(){
var selOption=document.getElementById("select");
selOption.selectedIndex=2;
}
</script>
<body>
<input type="button" onclick="clk()" value="click" />
<select name="select" id="select"  onchange="change()">
<option>aa</option>
<option>bb</option>
<option>cc</option>
</select>
</body>
问题:在页面选择能激发改变事件?通过按钮改变被选中项就不能激发事件如何在js代码改变被选中项激发,将激发什么事件呢

解决方案 »

  1.   

    我的个人看法是不会触发什么事件,这样就好<script language="javascript" type="text/javascript">
    function change(){
        alert("dd");
    }
    function clk(){
        var selOption=document.getElementById("select");
        selOption.selectedIndex=2;
        change();
    }
    </script>
      

  2.   

    在程序中改变选择项是不会触发select的onChange()事件的,必须显示地触发此时间,也就是在
    selOption.selectedIndex=2;
    后面加上一行
    [code=JScriptselOption.onchange()];[/code
    来显示触发,这样不管你在onChange()事件中指定任何方法,它都会自动调用了
      

  3.   

    在程序中改变选择项是不会触发select的onChange()事件的,必须显示地触发此时间,也就是在
    selOption.selectedIndex=2;
    后面加上一行
    selOption.onchange();
    来显示触发,这样不管你在onChange()事件中指定任何方法,它都会自动调用了
      

  4.   


    <script type="text/javascript">
    function change(){
        alert("dd");
    }
    function clk(){
        var selOption=document.getElementById("select");
        selOption.selectedIndex=2;
    }</script>
    <input type="button" onclick="clk()" value="click" />
    <select name="select" id="select"  onpropertychange="change()">
    <option>aa</option>
    <option>bb</option>
    <option>cc</option>
    </select>
      

  5.   

    你可以看下这段代码<input id="btn" type="button" onclick="clk()" value="click" />
    <script language="javascript" type="text/javascript">
    function clk(){
        alert('按钮被点击!');
        document.getElementById('btn').click();//这里的click()方法并不会触发onClick()事件(就像selOption.selectedIndex=2并不会触发onChange()事件一样),这种机制其实就是为了避免循环嵌套
    }
    </script>
      

  6.   

    +1.onchange和onpropertychange的不同之处在于onpropertychange是实时触发的,而onchange必须在当前元素失去焦点时才会触发。
      

  7.   

    你可以查看下手册说明,对onchange事件有明确的解释,事件被commit而不是value变化的时候触发,This event is fired when the contents are committed and not while the value is changing. For example, on a text box, this event is not fired while the user is typing, but rather when the user commits the change by leaving the text box that has focus. In addition, this event is executed before the code specified by onblur when the control is also losing the focus. The onchange event does not fire when the selected option of the select object is changed programatically.
    你可以看大概的表述,就是当选中项不是通过鼠标或者键盘点击改变的时候是不会触发的。