jquery在选择框中查找已禁用

时间:2017-01-12 08:47:38

标签: javascript jquery

我有三个选择框:

<select id="box1">   
    <option>foo1</option>   
</select>               

<select id="box2">
    <option>foo2</option>
</select>

<select id="box3">
    <option disabled>foo3</option>
</select>

我需要选择选项ID:禁用 - &gt; (BOX3)

如何找到选项的id:用jquery禁用?

3 个答案:

答案 0 :(得分:3)

您可以使用:disabled伪选择器来实现此目的

$('select option:disabled')

要获取父元素,您可以使用closest选择器或parent

&#13;
&#13;
console.log($('select option:disabled').closest('select').attr('id'));
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="box1">       
   <option disabled>foo1</option>
</select>

<select id="box2">       
   <option>foo1</option>
</select>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

&#13;
&#13;
alert($('option:disabled').parent().attr('id'))
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="box2">

</select>
<select id="box1">

</select>
<select id="box3">
  <option>foo1</option>
  <option>foo2
  </option>
  <option disabled>foo3</option>
</select>
&#13;
&#13;
&#13;

使用选择器:禁用

答案 2 :(得分:0)

var id = $("option:disabled").closest("selecte").attr('id');
console.log(id);