jeudi 4 août 2016

jquery check specific options in a multiselect

Alright I have a multiselect like this :

<select id="filtreMultiselectModif" name="groupes[]" multiple="multiple">

<script> 
$('#filtreMultiselectModif').multiselect(
    {
        includeSelectAllOption: true,
        enableFiltering: true,
        maxHeight: 200
    });
</script>

which give me that :

enter image description here

What I would like to do is to set checked some specific items that I store in a PHP table.

Here's my jquery code (that didn't work)

<script>
$(document).ready(function ()
{
    var arrayFromPHP = <?php echo json_encode($tableauIntitule); ?>;
    $('#filtreMultiselectModif').multiselect(
    {
        includeSelectAllOption: true,
        enableFiltering: true,
        maxHeight: 200
    });

    $('#filtreMultiselectModif option').each(function(i,option)
            {       
                $.each(arrayFromPHP,function(index,groupe){
                     if ($(option).text() === groupe) 
            {
                alert (option.text);
                 $(this).attr('checked', 'checked');
            }
                });
            });
});

Basicly I want every item stored in arrayFromPHP to be checked in the dropdown list but it doesn't seem to work..

Here's the code for my PHP array just in case :

<?php
$tableauIntitule = [];
$i = 0;
while ($data3 = mysqli_fetch_array($req3)) { // Remplissage du SELECT
  $intitule = $data3['intitule'];
  $tableauIntitule[$i] = $intitule;
}
?>




Aucun commentaire:

Enregistrer un commentaire