jQuery过滤器选项,其值大于(小于)变量

Jar*_*lav 1 javascript jquery

我需要禁用select中小于某个数字的选项值,这是我事先不知道的(在另一个表单元素中设置).我需要类似下面的代码,但变量值为"variableInteger":

select.children().filter(function() {
  return $(this).attr("value") > variableInteger;
}).each(function () {$(this).attr('disabled', 'disabled')});
Run Code Online (Sandbox Code Playgroud)

有一些聪明的方法吗?

PS.variableInteger值来自另一个表单元素,该名称仅在运行时也是已知的.

tym*_*eJV 8

不需要.each,也可以使用propthis.value(不需要$(this).attr("value");)

select.children("option").filter(function() {
    return this.value > variableInteger;
}).prop("disabled", true);
Run Code Online (Sandbox Code Playgroud)

  • 一个很好的选择,但我认为你没有回答这个问题.OP的代码应该可以工作([如此处所见](http://jsfiddle.net/fJ3vm/)),前提是`select`和`variableInteger`变量已正确设置 (2认同)