如何在For循环下获取选择的Multiselect选定值

时间:2016-04-20 04:46:15

标签: php jquery html jquery-chosen jquery-multiselect

这是我的查看文件代码

In [3]: df
Out[3]: 
   a  b  c  d
0  1  2  3  4
1  4  5  6  7
2  2  4  6  8
3  3  2  1  4
4  0  1  2  4

In [4]: cols = ['a', 'b', 'd']    # Columns to consider for average

In [5]: df['mean'] = df[cols].mean(axis=1)

In [7]: df
Out[7]: 
   a  b  c  d      mean
0  1  2  3  4  2.333333
1  4  5  6  7  5.333333
2  2  4  6  8  4.666667
3  3  2  1  4  3.000000
4  0  1  2  4  1.666667

我的脚本:当我从选项中选择一个或多个时,它不会进入脚本。如何在循环

下获取多个值
<?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 } ?> 

3 个答案:

答案 0 :(得分:2)

将文本更改为val()

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

或:

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

答案 1 :(得分:2)

请运行此示例代码。这可能会对你有所帮助

<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>

答案 2 :(得分:1)

在多选列表上使用.val()函数将返回所选值的数组:

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

并在您的html <select id="category" multiple="multiple">

同时将$ gen-&gt; genre_id的值放在一个变量中,就像这样

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

forloop中的<select id="category"也会有许多具有相同ID的select元素,将其更改为[$ i]类别或使用class