日期:2014-05-16  浏览次数:20545 次

如何使用js触发select的change事件?
网上搜了一下,都说用fireEvent,但是貌似不起作用啊~那可以怎么办呢?急用,thanks~
JScript code

                var list = document.getElementById('province');
        function setProvinceIndex() {
            for(var i=0; i<list.options.length; ++i) {
                if(list.options[i].value=='7') {
                    list.selectedIndex = i;
                    break;
                }
            }
            if(list.fireEvent) {
                list.fireEvent('onchange');
            } else if(list.change)
                list.change();
            }
        }


这里的list是一个select。

------解决方案--------------------
HTML code
<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <select id='province'>
        <option value='6'>66
        <option value='7'>77
        <option value='8'>88
    </select>
    <input type='button' onclick = 'setProvinceIndex();'  value='setPi'>
    <script type="text/javascript">
        var list = document.getElementById('province');
        function setProvinceIndex() {            
            /*for(var i=0; i<list.options.length; ++i) {
                if(list.options[i].value=='7') {
                    list.selectedIndex = i;
                    break;
                }
            }*/
            list.value = 7;
            if(list.fireEvent) {        //ie
                list.fireEvent('onchange');
            } else if(list.onchange){
                list.onchange();
            }
        }        
        list.onchange = function(){alert("event");}        
    </script>
</body>
</html>