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