Я не могу включить выбрать - PullRequest
0 голосов
/ 13 марта 2019

Привет У меня есть метод отключения выбора:

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 ]

2 голосов
/ 13 марта 2019

Установка для отключенного атрибута значения false на элементе управления 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>
0 голосов
/ 13 марта 2019

«Отключено» - это логический атрибут.

Наличие логического атрибута в элементе представляет истинное значение, а отсутствие атрибута представляет ложное значение.

Чтобы уточнить, неправильно устанавливать 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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...