導讀select第一個option選項選不中怎么回事_html/css_WEB-ITnose: 大工業用電 居民生活用電 一般工商業 農業生產用電 $('#user_type').on('change', function(e) { console.log(e.currentTarget.value); var index = e.currentTarget.options.selectedIn
$('#user_type').on('change', function(e) { console.log(e.currentTarget.value); var index = e.currentTarget.options.selectedIndex; _eleTypeCode = e.currentTarget.value; var text = e.currentTarget.options[index].text; $("#user").val(text); });
回復討論(解決方案)
有沒有人么啊
change事件是在選項改變時才會觸發,頁面加載后默認就是第一項被選中,這時你點了第一項,選項并沒有改變,事件就不會被觸發 可以在頁面加載后讓它沒有選項被選中 $('#user_type')[0].selectedIndex = -1; $('#user_type').on('change', function(e) { console.log(e.currentTarget.value); var index = e.currentTarget.options.selectedIndex; _eleTypeCode = e.currentTarget.value; var text = e.currentTarget.options[index].text; $("#user").val(text); });
實測可以選中任意一項, 但頁面初始化時, 不會觸發 onchange 事件, 所以也不會把 user 輸入框的文本自動設置為第一項的值