[問題] 怎麼修改option的text

作者: knives   2013-12-19 18:17:21
我有一個需求需要 讓 <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
一定得我要手動點網頁其他地方,才會變成兩個字
請問該怎麼改才好
謝謝回覆
作者: akiratw   2013-02-19 21:44:00
http://jsfiddle.net/9kAPr/1/ 這樣嗎?不過我不知道你原本的錯在哪XD
作者: DeathWatch (愛情模糊理論)   2013-02-19 21:49:00
綁onchange?
作者: knives   2013-02-20 08:21:00
好了,可是還是不知道那裡錯

Links booklink

Contact Us: admin [ a t ] ucptt.com