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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
}

至此解决。。

以上是关于js设置下拉框选中后change事件无效解决的主要内容,如果未能解决你的问题,请参考以下文章

js配置下拉框默认值无效

如何触发下拉框下拉事件

js 下拉框获取焦点后 ,自动展开选项内容?

jquery change事件 在弹窗中无效

select 下拉框的选中项的change事件

easyui combobox 用代码赋值不触发change事件,选择值会触发