Toggle dropdownlist enable and disable with a butt

2019-02-20 11:04发布

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?

2条回答
Rolldiameter
2楼-- · 2019-02-20 11:29

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
}
查看更多
地球回转人心会变
3楼-- · 2019-02-20 11:43
function toggleDisableDropDown(DropDownID)
{
  var sel = document.getElementById(DropDownID);
  sel.disabled = !sel.disabled;
  return false; 
}
查看更多
登录 后发表回答