Re: [問題] 下拉式選單和multiple select
怎麼會沒效呢 ~"~?
可能是說得不夠清楚, 附個範例:
<title>select test</title>
<script type="text/javascript"
src="" rel="nofollow">http://code.jquery.com/jquery-latest.pack.js"></script>
<select multiple="multiple">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<script type="text/javascript">
// 選擇所有 multiple select
// 綁定 mousedown 事件的 listener
// 針對的對項是 select 其下的 option
$('select[multiple="multiple"]').on('mousedown', 'option', function(event) {
// 阻止 mousedown 預設的後續行為
// ( 其中最少應該包括了:
// -> 判斷有沒有 ctrl key, 決定是否要取消其他選擇
// -> 選擇當下按到的
// -> 對 <select> 設定 focus )
// 但不阻止 event bubbling
event.preventDefault();
// 設定當下點到的目標
// 沒選的變成有選的, 有選到的變成沒選的
event.currentTarget.selected = !event.currentTarget.selected;
// 重新對 select 物件設定 focus, 註解這行就知道差別了 ~"~
$(event.delegateTarget).focus();
});
</script>
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.45.190.210
→
04/10 23:44, , 1F
04/10 23:44, 1F
→
04/10 23:45, , 2F
04/10 23:45, 2F
→
04/11 02:04, , 3F
04/11 02:04, 3F
嗯, 看來您用的是 IE, 這是瀏覽器問題, 先天限制:
http://www.highdots.com/forums/jquery/
select-option-not-catching-mousedown-281439.html
( 縮了以後是: http://ppt.cc/83E6 )
簡單說就是 IE 的 option 上面收不到 mousedown 的 event,
所以要能在 IE 上跑的話可能就沒辦法這樣子偷懶去攔截 event,
真的就是需要去紀錄那些是已經選擇的, 然後自動重新選擇..
但那樣子應該就無法避免可能的閃爍了 :(
( Chrome 18.0.1025.152 m 是可以正確運作的 )
→
04/11 09:00, , 4F
04/11 09:00, 4F
※ 編輯: Crow22312 來自: 114.45.189.47 (04/11 21:53)
想一想還有一種可能, select 收到 mousedown 之後用 X, Y 座標
很搞剛的去判斷按在哪個 option 上面, 然後修改, preventDefault()..
※ 編輯: Crow22312 來自: 114.45.189.47 (04/11 21:57)
討論串 (同標題文章)
完整討論串 (本文為第 3 之 4 篇):