简体   繁体   中英

How to get Chosen Multiselect selected values under the For Loop

This is my view file code

<?php for($i=0; $i<4; $i++)  { ?>
 <div class="box22">
      <div class="mcm">
           <input type="text" placeholder="Myself" id="coworkers" name="coworkers[]" />
           <span class="bar"></span>
      </div>

      <div class="select2">
       <select id="category_<?php echo $i; ?>" name="category[]" class="chosen-select ref-sel1" multiple >
           <?php
           foreach($genre as $gen){
                echo '<option value='.$gen->genre_id.'>'.$gen->genre_name.'</option>';
           } 
           ?>
       </select>
      </div>
 </div>
<?php } ?> 

my script : when i chose one or more from option, it does not comes into script. How to get multiple values under loop

    $(document).ready(function()
    {
        $('form#shortfilm').submit(function(e) 
        {
            e.preventDefault();
            var form = $(this);
            var foo = [];
            $('#category :selected').each(function(i, selected){
              foo[i] = $(selected).text();
            });
        });
    });

change text to val()

 $('option:selected').each(function(i, selected){
              foo.push($(selected).val());
            });

or:

var foo = [];
$('.box22').each(function(x,v){
var temp =[]
     $(v).find('option:selected').each(function(i, selected){
        temp.push($(selected).val());
     });
     foo.push(temp)
});

see demo for the second option here

Please run this sample code. This may help you

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
</head>
<body>
<form id="shortfilm" name="data">
<?php $genre=array(1=>'AAAAAAAAAAAAAA',2=>'BBBBBBBBBBBBBBB',3=>'CCCCCCCCC',4=>'DDDDDDDDDDDDDD',5=>'EEEEEEEEEEEEEEE');
for($i=0; $i<4; $i++)  { ?>
 <div class="box22">
      <div class="mcm">
           <input type="text" placeholder="Myself" id="coworkers" name="coworkers<?php echo$i?>[]" />
           <span class="bar"></span>
      </div>

      <div class="select2">
       <select class="category" name="category<?php echo$i?>[]" class="chosen-select ref-sel1" multiple >
           <?php
           foreach($genre as $key => $gen){
                echo '<option value='.$key.'>'.$gen.'</option>';
           } 
           ?>
       </select>
      </div>
 </div>
<?php } ?> 
<input type="submit" value="submit" />
</form>
<script>
$(document).ready(function()
{
    $('form#shortfilm').submit(function(e) 
    {
        e.preventDefault();
        var form = $(this);
        var foo = [];
        $('.category :selected').each(function(i, selected){
          foo[i] = $(selected).text();
        });
        console.log(foo);
    });
});
</script>
</body>
</html>

Using the .val() function on a multi-select list will return an array of the selected values:

var selectedValues = $('#category').val();

And in your html <select id="category" multiple="multiple">

Also put the values of the $gen->genre_id in a variable and so like this

foreach($genre as $gen){
$genId = $gen->genre_id;
$genName = $gen->genre_name;
            echo '<option value='.$genId.'>'.$genName.'</option>';
       } 

Also <select id="category" in a forloop will have many select elements with the same id,change that to category[$i] or use class

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