2012年8月8日 星期三

jQuery - 如何隱藏 select 下的 option. How to hide the options in select using jQuery.

$("#sel option").hide();    ?

沒辦法耶…

因為hide 不是跨瀏覽器的,至少 IE 8 無法達到這個效果。

網路上有一些solution,但是要以plugin 的方式進行,我覺得沒有必要。

最簡單的原則就是 「把option 搬走」

先宣告變數來裝。


var from_text="" ;
var to_text="";


$("#from_year").change(function(){
            if($(this).val()==''){                                                 
                from_text = $("#from_Q").html();                
                $("#from_Q option").remove();
            }else{
                $("#from_Q").html(from_text);
            }
        });

如果年選成空白的時候,把第幾季(Q1-Q4的select)的選擇搬走。
如果年選回某一年的時候,再把第幾季的文字加回來即可。


沒有留言:

張貼留言