2013-02-08 72 views

回答

0
$("select").find("option").each(function(i,e) { 

      var t=$(e).text(); 
      if (t.replace(/^\-/,'')==t) e.disabled=true; 
      }); 

編輯的0指數其實更好,但隨後e.disabled更兼容的瀏覽器; &發現是快

所以

$("select").find("option").each(function(i,e) { 

      var t=$(e).text()['0']; 
      if (t!=='-') e.disabled=true; 
      }); 
+0

雅我是但我在你的評論之前看到它:) – mikakun

+0

downvote爲最優化的代碼段?真的嗎? – mikakun

+0

不,downvote是完全和完全不必要的使用'replace'。至於你的編輯,你至少已經處理了。 (即使如此,這是*不是*「最優化片段」。) –

0
$("YourSELECT options").each(function(){ 
    if(!$(this).html().startsWith("-")) 
    { 
     $(this).attr("disabled","true"); 
    } 
}); 

String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)} 
3

就在零index文本的檢查字符如果-與否

Live Demo

$('#selID option').each(function(){ 
    if($(this).text()[0] != '-') 
     this.disabled=true; 
}); 
+0

FWIW,請注意,IE7和更早版本不支持在字符串上使用[]。但是,我們真的不應該支持IE7和更早的版本。 :-) –

+1

感謝@ T.J.Crowder的好建議,發生了變化。 – Adil

相關問題