douzhantao2857 2017-06-19 23:32
浏览 55

下拉菜单打开另一个下拉菜单 - CodeIgniter

I have a dropdown menu that I supplied for foreach statement for and on select I need it open up another drop down menu to specify the items

DropDown 1 - Categories (All Done)

On Click of Category

Open all the products that are associated with that Category.

Should I do a Ajax request and then in the controller select all products under that category - return the array and then supply a foreach statement to select those products?

Would appreciate also links where something like this has been done before.

  • 写回答

1条回答 默认 最新

  • dongxuan8227 2017-06-20 03:48
    关注

    Here is a snippet code i used to create ajax driven multiple drop down menu. Actually i have 2 methods for sake of simplicity i am posting simplest method ie one that doesn't has json manipulations.

    In the view page

     <select class="form-control" name="categories" id="categories" onchange="load_products()" > 
         <option value="" selected="selected"><?php echo "Select A Category" ; ?></option> 
     </select> 
    
     <select class="form-control" name="products" id="products" required/></select>
    

    JavaScript code in view page

    <script> 
        function load_products() 
        { 
            categories=$('#categories').val(); 
    
            if($('#categories').val()!="" && $('#categories').val()!="*") { 
    
                $.ajax({ 
                    type: "POST", 
                    url: " <?php echo base_url('Home/load_products'); ?>", 
                    data: "categories=" + categories, 
                    success: function (msg) { 
                        if (msg) { 
                            $('#products').html(msg); 
                          //  $('#products').removeClass('hidden') 
                        } 
                        } 
                }); 
               // return false; 
            } 
            else { 
                $('#products').html(''); 
         //   $('#products').addClass('hidden') 
    
            } 
        } 
    </script> 
    

    Now in the controller

          function load_products() 
            { 
                $categories=$this->input->post('categories'); 
    
                $products=$this->Homemodel->allproducts($categories); 
    
                if($products!=NULL) 
                { 
                    $fop="Select A Product "; 
                  echo ($this->create_drop_down($fop,$products,'productname','id')); 
                } 
                else{ 
                    echo "<option value=''>No Product Found</option>"; 
    
                } 
            } 
    
          function create_drop_down($options,$opts,$txt,$val) 
           { 
                $options = "<option value=''>".$options."</option>"; 
    
                if(!empty($opts)) 
                { 
                    foreach ($opts as $row) 
                    { 
    
                        $options.="<option value='".$row->$val."'>".$row->$txt."</option>"; 
                    } 
                } 
                return $options; 
            } 
    

    I am skipping model code. If this method doe not work for you comment below.

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示