js设置下拉框选中后change事件无效解决


下拉框部分代码:

<select id="bigType">
     <option value="">请选择</option>
     <option value="1">xiamen</option>
     <option value="2">beijing</option>
</select>

<select id="smallType">
     <option value="">请选择</option>
</select>

如果给"bigType"的下拉框添加change事件来动态改变"smallType"下拉框的值的话,代码如下:

jQuery("#bigType").change(function(){
    //do something
});

那么,通过js设置"bigType"某项选中后,如:

jQuery("#bigType option[value="1"]").attr("selected","selected")
//jQuery("#bigType option:contains("xiamen")").attr("selected","selected")

该change事件不会自动触发,解决办法:

自定义change方法,在下拉框中添加onchage事件并传参(当前选中的value值),自定义调用时间:

<select id="bigType" onChange="getVariety(this.options[this.selectedIndex].value)">
     <option value="">请选择</option>
</select>
function getVariety(val){
     //set some options checked
}

至此解决。。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM