下拉框部分代碼:
<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 }
至此解決。。