【问题标题】:check if prop of selected dropdown is disabled检查所选下拉列表的道具是否被禁用
【发布时间】:2017-01-03 11:13:46
【问题描述】:

我无法检查下拉菜单的选定选项是否被禁用。

用户可以选择一个选项,然后选择一个时间范围,在时间选择后,该范围内所有不可用的选项将被设置为禁用。如果先前选择的值也被禁用,则必须发出警报。

我在想这样的事情:

if($('#dropdown').val().prop('disabled',true)){
alert('not possible'); 
}

【问题讨论】:

  • 您可以在select 框中选择禁用的选项吗?
  • 是的,因为该选项只有在用户输入日期范围后才会被禁用。第一个答案对我有用

标签: javascript jquery html-select


【解决方案1】:

使用这个:

if($('#dropdown').find(':selected').prop('disabled')){
  alert('not possible'); 
}

$('input').change(function(){
  if($(this).val()>50){
    $('select option:first-child').prop('disabled',true);
  }
  if($('select').find(':selected').prop('disabled')){
      alert('not possible'); 
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>
<input type="range"/>

【讨论】:

  • @nogato,别忘了投票或接受回答!请看这里:meta.stackexchange.com/questions/5234/…,以帮助其他人。
  • 是的,我不会。我不能投票,因为我缺乏声誉,而且我需要等待一段时间才能接受答案:-)
  • @nogato,我用一个例子更新了我的答案。当您滑动超过 50 次时,选择的选项将被禁用。
【解决方案2】:

您可以获取:selected 选项,然后检查其prop()

if($('#dropdown option:selected').prop('disabled') == true){
    //Selected option is disabled
}

【讨论】:

    【解决方案3】:

    检查选中和禁用选项的计数。使用:selected:disabled 伪类选择器来选择选项。

    if($('#dropdown option:selected:disabled').length){
       alert('not possible'); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 2022-01-20
      相关资源
      最近更新 更多