[問題] 怎麼修改option的text

看板Ajax作者時間11年前 (2013/12/19 18:17), 編輯推噓2(202)
留言4則, 3人參與, 最新討論串1/1
我有一個需求需要 讓 <select> 如果沒有選擇的時候 將裡面的option 裡面的text只顯示前兩個字 當該select 被選擇到的時候,又會恢復原來的字元 然後離開select 的時候,又只顯示前兩個字 我目前是這樣寫 $('option').each(function(){ //一開始載入的時候,把原來的字元存到old_text var old_text = $(this).text(); $(this).attr('old_text',old_text); $(this).text(old_text.substring(0,2)); }); $('select').click(function(){ //當按下select,還原字元 $(this).children('option').each(function(){ //$(this).attr('old_text',old_text); $(this).text($(this).attr('old_text')); }); }); $('select').change(function(){ $(this).children('option').each(function(){ $(this).text($(this).attr('old_text').substring(0,2)); }); }); $('select').blur(function(){//離開的時候,又再變成兩個字 $(this).children('option').each(function(){ $(this).text($(this).attr('old_text').substring(0,2)); }); }); 問題來了,當我選擇了某一個選項之後,並不會馬上 做blur或change 一定得我要手動點網頁其他地方,才會變成兩個字 請問該怎麼改才好 謝謝回覆 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 42.75.115.37

12/19 21:44, , 1F
12/19 21:44, 1F

12/19 21:45, , 2F
不過我不知道你原本的錯在哪XD
12/19 21:45, 2F

12/19 21:49, , 3F
綁onchange?
12/19 21:49, 3F

12/20 08:21, , 4F
好了,可是還是不知道那裡錯
12/20 08:21, 4F
文章代碼(AID): #1IiiUqQy (Ajax)
文章代碼(AID): #1IiiUqQy (Ajax)