简体   繁体   中英

Javascript + PHP - How to check a checkbox when user select a file to upload

I have an input file list from database:

<?php

for ($x=0; $x<count($Permisos); $x++) {


?>

<tr>
     <td> <?php echo utf8_encode($Permisos[$x]['des_permiso']); ?></td>
     <td> <input name="txt_arch_<? echo $x;?>" type="file" class="text" id="txt_arch_<? echo $x;?>"> </td>
      <td> <input name="cbx_<? echo $x;?>" type="checkbox" class="text" id="cbx_<? echo $x;?>" value="S"> 

      </td>
</tr>

<?php

}

?>

This code returns a list of 'permisos' (requirements) with a checkbox next, for check it to upload the file.

What i have to do is: when user select a file (id or name: "txt_arch_<? echo $x;?>" ), the checkbox ( "cbx_<? echo $x;?>" ) will check it automatically.

I don't know if if explain very well.

Thank you for answers.

Add this to the document.ready event, assuming your using the jQuery framework:

$('input[type="file"]').on('change', 
  function()
  {
    $(this).next('input[type="checkbox"]').prop('checked', true);
  }
); 

I solved it:

// .....
<input onChange="VerifyFctn('<?php echo $x; ?>')" name="txt_arch_<? echo $x;?>" type="file" class="text" id="txt_arch_<? echo $x;?>">
// .....

Javascript:

function VerifyFctn(id){
    document.getElementById("cbx_"+id).checked = true;
}

Anyway, thank you for see the post :)

Greetings

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