2013-02-08 70 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

相关问题