简体   繁体   中英

Im trying to disable/enable a dropdown when button is clicked

Im trying to disable/enable a dropdown box once a button is clicked. I tried the following but it's not working:

function disable() {
  document.getElementById('room').disabled=true;
}
function enable() {
  document.getElementById('room').disabled=false;
}

My dropdown is inside a <?php?> which is this:

print "<select  class=\"no_of_room\"name=\"qtyroom".$sub_row2['room_id']."\"  
id=\"room".$sub_row2['room_id']."\" 
onChange=\"selection(".$sub_row2['room_id'].")\" style=\"width:100%; 
color:black; height:45px;\">\n";
print "<option value=\"0\">0</option>\n";
$i = 1;
while($i <= $sub_row2['total_room'])
{
 print "<option value=\"".$i."\">".$i."</option>\n";    
 $i = $i+1;
 }
 print "</select>\n";

Then

print "<input type=\"button\" onclick=\"enable()\" value=\"Enable list\">";
print "<input type=\"button\" onclick=\"disable()\" value=\"Disable list\">";

Your enable/disable functions are trying to work with element id "room" while your select element id has something attached to it room".$sub_row2['room_id']

In order for this to work IDs have to be the same.

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