dongwei1954 2017-05-18 10:11
浏览 60
已采纳

从下拉列表中仅获取选定的值,并从下拉列表中删除未选中的值

I have two multiple dropdown in which one dropdown i have all the fields with all the values and in second drop down i have shown the selected value but with this all the other values are also coming which i have not selected ,i want to show only those three values which i have selected at the time of add record and values are saved comma separately in db(1,2,3). Below is my php code which shows selected value as well as all the other value also.So i want to remove that values from the second dropdown which i have not added.Where $state is a array which shows all the list of with there id and name.

  <div class="form-group end">
                <select id="second" multiple="true" name="states[]" id="states" >
                <?php
                    foreach($states as $key => $val){
                        $value = $val->id;
                        $name = $val->name;
                        $selected = 1,2,3;
                        $selected_values = explode(",",$selected);
                        echo "<option value='$value'".((in_array($value,$selected_values)) ? " selected='selected'":"").">$name</option>";
                     }
                     //var_dump($name);die;
                    ?>
               </select>

My multiple drop down is like this http://jsfiddle.net/h8zuc/

  • 写回答

1条回答 默认 最新

  • dongyin2643 2017-05-18 10:33
    关注

    try this: I am asuming this is your second dropdown:

    <div class="form-group end">
        <select id="second" multiple="true" name="states[]" id="states" >
            <?php
            foreach($states as $key => $val){
            $value = $val->id;
                    $name = $val->name;
                    $selected = "1,2,3";
                    $selected_values = explode(",",$selected);
                    if(in_array($value,$selected_values)){ 
                        echo "<option value='".$value."' selected='selected'>$name</option>"; 
                    }
                 }
                 //var_dump($name);die;
                ?>
        </select>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题