dongmi1941 2014-11-12 16:45
浏览 24
已采纳

从选择倍数中获取值

So i have a multi-select which I've adapted with js to work without the CTRL key

$(function(){
    $('.select-toggle').each(function(){    
        var select = $(this), values = {};    
        $('option',select).each(function(i, option){
            values[option.value] = option.selected;        
        }).click(function(event){        
            values[this.value] = !values[this.value];
            $('option',select).each(function(i, option){            
                option.selected = values[option.value];        
            });    
        });
    }); 
});

and i'm using it on multiple input fields, an example below.

<tr class="wide">
   <td class="input titledinput" style="left: -5px;"><select title="Please select the Consoles/Platforms the game is going to be, or has, released on. You can select more than one."
    multiple="multiple" class="select-toggle roundedcorners" name="Consoles" size="1"
    />';
       $qselectconsole = "SELECT * FROM console ORDER BY Console asc";
       $rselectconsole = mysqli_query($dbc, $qselectconsole);
       if(mysqli_num_rows($rselectconsole) !== false){
         while($rowselectconsole = mysqli_fetch_array($rselectconsole, MYSQLI_ASSOC)){
           echo '<option value="'.$rowselectconsole['ConsoleID'].'">'.$rowselectconsole['Console'].'</option>'; 
         }
       } else {
          echo '<option value="0">Something went wrong, please try refreshing the page</option>';
       }
          echo '</select>
   <p2>Platform</p2></td>
</tr>

The multi-select works as expected. Now i would expect $_POST['Consoles'], in this case, to output the value of each selected value. But when i use $_POST['Consoles'] it outputs only the last variable to be selected. How would i go about getting all of the selected values out (as i'll need them later for input to the database).

I am open to a complete rehaul of my JS if needed, i'm an inexperienced javascript programmer and honestly don't really know what i'm doing when it comes to js. I'll learn as i go but if i've done something stupid that means I can only select multiple options but not store each individual value then please let me know.

  • 写回答

1条回答 默认 最新

  • drwpbrv668670 2014-11-12 16:52
    关注

    First you need to change the name of the var:

    name="Consoles"
    

    with:

    name="Consoles[]"
    

    This will make possible on php to have all the options into a single "array" now if you on php write:

    print_r ($_POST['Consoles']);
    

    You can see each var selected on html page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能