Toggle dropdownlist enable and disable with a butt

2019-02-20 11:21发布

问题:

I have the following JavaScript to disable a dropdownlist in a ASP.NET page, which gets called when I click a button.

function disableDropDown(DropDownID)
{
  document.getElementById(DropDownID).disabled = true;
  return false; 
}

I wanted to use the same button to toggle between enable and disable for that dropdownlist. How do I do this?

回答1:

You just have to invert the boolean disabled attribute:

function toggleDisableDropDown(dropDownID) {
  var element = document.getElementById(dropDownID); // get the DOM element

  if (element) { // element found
    element.disabled = !element.disabled; // invert the boolean attribute
  }

  return false; // prevent default action
}


回答2:

function toggleDisableDropDown(DropDownID)
{
  var sel = document.getElementById(DropDownID);
  sel.disabled = !sel.disabled;
  return false; 
}