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
}

至此解决。。

转载于:https://my.oschina.net/caiya928/blog/784034

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值