简体   繁体   中英

remove disabled didn't work

i have some script from php loop

<?php
for($y=1;$y<=5;$y++){
echo"
<script>
 function check_$y(val)
{       
    if(val == '3'){
        document.getElementById('sbg_$y').disabled=false; 
    }else{
        document.getElementById('sbg_$y').disabled=true;
    }
}
</script>




<select name='as_$y' id='c_$y' onchange='check_$y(this.value)'>
   <option value='talent'>talent</option>
   <option value='produser'>produser</option>
   <option value='writer'>penulis</option>
   <option value='actor'>actor</option>
</select>       

<input size='20' id='sbg_$y' type='text' name='sebagai_$y' disabled>

";//end of echo



}//end of loop

?>

i want if actor selected id='sbg_$x' is enable, but i don't know why that function isn't work i guess it caused by php loop,

anyone can solve it ? i'm new in javascript

You have given all values as texts...so you have to match with text

Do it like this...

function check_$y(val){    
     if(val == 'actor'){
         document.getElementById('sbg_$y').disabled=false; 
     }else{
         document.getElementById('sbg_$y').disabled=true;
     }
}

or you can match the selected index...

<select name='as_$y' id='c_$y' onchange='check_$y(this)'>
   <option value='talent'>talent</option>
   <option value='produser'>produser</option>
   <option value='writer'>penulis</option>
   <option value='actor'>actor</option>
</select>

function check_$y(selectBox){    
     if(selectBox.selectedIndex == '3'){
         document.getElementById('sbg_$y').disabled=false; 
     }else{
         document.getElementById('sbg_$y').disabled=true;
     }
}

<script>
 function check_$y(val)
{       
    if(val == 'actor'){
        document.getElementById('sbg_$y').disabled=false; 
    }else{
        document.getElementById('sbg_$y').disabled=true;
    }
}
</script>

Then Start loop

<?php

for($y=1;$y<=5;$y++){
<select name='as_$y' id='c_$y' onchange='check_$y(this.value)'>
   <option value='talent'>talent</option>
   <option value='produser'>produser</option>
   <option value='writer'>penulis</option>
   <option value='actor'>actor</option>
</select>       

<input size='20' id='sbg_$y' type='text' name='sebagai_$y' disabled>

";//end of echo
}
?>

}

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