简体   繁体   中英

Toggle dropdownlist enable and disable with a button click using Javascript?

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?

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
}
function toggleDisableDropDown(DropDownID)
{
  var sel = document.getElementById(DropDownID);
  sel.disabled = !sel.disabled;
  return false; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM