【问题标题】:How to push the values with open brace and close brace如何用左大括号和右大括号推送值
【发布时间】:2016-04-20 06:04:44
【问题描述】:

我的答案显示为 1,2,3,1,2,1,4,5(没有打开和关闭标签)

但是我想展示这个模型[1,2,3][1,2][1,4,5]

想要打开关闭标签,因为想要分隔选项值以存储在数据库中。

我的脚本

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

我的查看文件代码

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

【问题讨论】:

  • 这个脚本的输出是什么?
  • 在警报中显示 1,2,3,1,2,1,4,5
  • @shruthi 为什么需要提醒这些数据?
  • 请张贴html代码

标签: jquery push


【解决方案1】:

试试下面的

var foo = [];
  $('.box22').each(function(x, v) {
    var temp = []
    $(v).find("select[name='category[]'] > option:selected").each(function(i, selected) {
      temp.push($(selected).val());
    });
    text = '[' + temp.join(',') + ']'
    foo.push(text);

  });
  alert(foo.join(''));

demo here

【讨论】:

  • 谢谢。从这里得到答案。
【解决方案2】:

请用这个

var foo = [];
            $('.box22').each(function(x,v){
            var temp =[];   
             $(v).find('select  option').each(function() {
                 var selected = $(this).attr("selected");
                 var selectedvalue = $(this).attr("value");
                if(selected = "selected")
                {
                    temp.push(selectedvalue);
                }
             });
            foo[x] = temp;
                 console.log(temp);
            });

【讨论】:

    【解决方案3】:

    试试这个

    var foo = new Array();
     var temp =new Array();
    $('.box22').each(function(x,v){
    
       $(v).find("select[name='category[]'] > option:selected").each(function(i, selected){
    
           temp={
               p: '[' +$(selected).val()+']',
                  }
             });
       foo.push(temp.p);
       alert(foo);
    });
    

    【讨论】:

    • $(selected).val(); goth 是什么键?
    猜你喜欢
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 2019-02-03
    • 2011-09-26
    • 2021-09-15
    • 1970-01-01
    相关资源
    最近更新 更多