我无法启用选择

时间:2019-03-13 17:04:38

标签: javascript jquery html web

嗨,我有一种禁用选择的方法:

function setPersons(adultos){
    document.getElementById("perselect").value = adultos; 
    document.getElementById("perselect").disabled = true;
}

当我加载我的网站时被称为。这项工作没有问题。

我的问题是当我尝试通过表单内的按钮再次启用它时。

这是按钮代码:

<button class="button-big" name="disp" value="clean" id="save" type="button">Limpiar fecha</button>

这是jQuery中捕获它的方法:

$('#save').on('click', function(e) {
    document.getElementById("perselect").disabled=false;
    document.getElementById("ninselect").disabled=false;                    
});

当页面加载时,选择被禁用,但是如果按下按钮,则会调用click事件(我在其中放了一个警报来检查它),但是选择始终被禁用。

会发生什么?

2 个答案:

答案 0 :(得分:2)

false控件上将Disabled属性设置为select实际上并没有重新启用该控件。当disabled属性被完全删除时,大多数浏览器将重新启用控件:

$(document).ready(function() {
  document.getElementById("combobox").disabled = true;
});

$("#btn-enable").on('click', function() {
  document.getElementById("combobox").removeAttribute("disabled");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="btn-enable">Enable</button>
<select id="combobox">
  <option>1</option>
  <option>2</option>
  <option>3</option>
</select>

答案 1 :(得分:0)

“已禁用”是布尔属性。

  

元素上存在布尔属性表示真实值,而缺少属性则表示错误值。

为了进一步详细说明,将Element-dot-Disabled设置为true是错误的

$(document).ready(function() {
  document.getElementById("combobox").setAttribute("disabled","");

});

$("#btn-enable").on('click', function() {
  document.getElementById("combobox").removeAttribute("disabled");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<label>
  <select checked name=one disabled> 
    <option>abc</option>
    <option>123</option>
  </select>
</label>observe that "disabled" attribute is present in the tag

<br>

<label>
  <select checked name=two> 
    <option>def</option>
    <option>456</option>
  </select>
</label>observe that "disabled" attribute is NOT present in the tag

<br>
<br>
Your code snippet:
<br>

<label>
  <select checked name=combobox id=combobox> 
    <option>ghi</option>
    <option>7890</option>
  </select>
</label>Boolean attributes are considered to be true if they're present on the element at all, regardless of their actual value; as a rule, you should specify the empty string ("") in value (not null)when using Element-dot-setAttribute

<br>

<button id="btn-enable">Enable it Button!</button>

了解更多信息,请访问 https://www.w3.org/TR/html5/infrastructure.html#sec-boolean-attributes