我想在更改主下拉菜单时构建更新表单然后更新第二个下拉列表

Posted

技术标签:

【中文标题】我想在更改主下拉菜单时构建更新表单然后更新第二个下拉列表【英文标题】:I Want To Build Update Form When I Change Main Dropdown Menu Then update the second dropdown list 【发布时间】:2015-09-03 20:35:58 【问题描述】:
                     <div>
                        <label for="form-field-select-3">State : </label>
                        <br />
                        <select class="chosen-select form-control" id="State" name="State" data-placeholder="Choose a State...">
                            <option value="0"> </option>
                            <?php
                             $query=$this->db->query("select Address_State from ref_address_state  where Address_State_ID='".$property->State_ID."'");
                             $query=$query->row();   
                             foreach($Address_State as $rows)
                             
                                if($rows['Address_State']==$query->Address_State)
                                   

                                            echo "<option value='".$rows['Address_State_ID']."' selected>".$rows['Address_State']."</option>";                                                                    
                                  
                            else
                                  
                                           echo "<option value='".$rows['Address_State_ID']."'>".$rows['Address_State']."</option>";                                                                    

                                  
                                   
                             ?>
                        </select>
                    </div>
                     <div>
                        <label for="form-field-select-3">City : </label>
                        <br />
                        <select class="chosen-select form-control" id="City" name="City" data-placeholder="Choose a City...">
                            <option value="0">  </option>
                           <?php
                           $query=$this->db->query("select city_name from ref_city  where city_id='".$property->City."'");
                             $query=$query->row();   
                             foreach($city_name as $rows)
                             
                                if($rows['city_name']==$query->city_name)
                                   

                                     echo "<option value='".$rows['city_id']."' selected>".$rows['city_name']."</option>";                                                                    
                                    
                                    else
                                    
                                    echo "<option value='".$rows['city_id']."'>".$rows['city_name']."</option>";                                                                    

                                    
                             
                                  ?>

                        </select>
                    </div>
                    <div>
                        <label for="form-field-select-3">Locality : </label>
                        <br />
                        <select class="chosen-select form-control" id="Locality" name="Locality" data-placeholder="Choose a Location...">
                            <option value="0">  </option>
                               <?php
                             $query=$this->db->query("select locality_name from ref_locality  where locality_id='".$property->Locality."'");
                             $query=$query->row();   
                             foreach($locality_name as $rows)
                             
                                if($rows['locality_name']==$query->locality_name)
                                   

                                     echo "<option value='".$rows['locality_id']."' selected>".$rows['locality_name']."</option>";                                                                    
                                    
                                    else
                                    
                                    echo "<option value='".$rows['locality_id']."'>".$rows['locality_name']."</option>";                                                                    

                                    
                             
                                  ?>
                        </select>
                    </div>
                    </div>
                </div>
            </div>
    </div>

【问题讨论】:

请详细说明您希望我们做什么 【参考方案1】:

我想你想要这样的东西

http://www.91weblessons.com/codeigniter-ajax-country-state-city-drop-down/

javascript

 <script>
    function selectState(country_id)//on change of country select box
    
      if(country_id!="-1")
        loadData('state',country_id);
        $("#city_dropdown").html("<option value='-1'>Select city</option>");
      else
        $("#state_dropdown").html("<option value='-1'>Select state</option>");
        $("#city_dropdown").html("<option value='-1'>Select city</option>");
      
    

    function selectCity(state_id)//on change of state select box
    
      if(state_id!="-1")
       loadData('city',state_id);
      else
       $("#city_dropdown").html("<option value='-1'>Select city</option>");
      
    


    function loadData(loadType,loadId)
      var dataString = 'loadType='+ loadType +'&loadId='+ loadId;
      $("#"+loadType+"_loader").show();
      $("#"+loadType+"_loader").fadeIn(400).html('Please wait... <img src="image/loading.gif" />');
      $.ajax(
        type: "POST",
        url: "loadData",
        data: dataString,
        cache: false,
        success: function(result)
          $("#"+loadType+"_loader").hide();
          $("#"+loadType+"_dropdown").html("<option value='-1'>Select "+loadType+"</option>");
          $("#"+loadType+"_dropdown").append(result);
        
     );
    

    </script>

控制器:

<?php

    public function index()
    
       $this->load->model('model');
       $result['list']=$this->model->getCountry();
       $this->load->view('top');
       $this->load->view('index',$result);
       $this->load->view('footer');
    

     public function loadData()
     
       $loadType=$_POST['loadType'];
       $loadId=$_POST['loadId'];

       $this->load->model('model');
       $result=$this->model->getData($loadType,$loadId);
       $HTML="";

       if($result->num_rows() > 0)
         foreach($result->result() as $list)
           $HTML.="<option value='".$list->id."'>".$list->name."</option>";
         
       
       echo $HTML;
     
    ?>

型号:

<?php
function getCountry()

   $this->db->select('id,country_name');
   $this->db->from('country');
   $this->db->order_by('country_name', 'asc');
   $query=$this->db->get();
   return $query;


function getData($loadType,$loadId)

   if($loadType=="state")
    $fieldList='id,state_name as name';
    $table='state';
    $fieldName='country_id';
    $orderByField='state_name';
   else
    $fieldList='id,city_name as name';
    $table='city';
    $fieldName='state_id';
    $orderByField='city_name';
   
   $this->db->select($fieldList);
   $this->db->from($table);
   $this->db->where($fieldName, $loadId);
   $this->db->order_by($orderByField, 'asc');
   $query=$this->db->get();
   return $query;
 

?>

【讨论】:

请在您的帖子中添加解决方案,而不仅仅是链接,以防链接失效。 @Dakkaron 实际上我很清楚我只放链接但现在已经编辑的问题 每当您使用链接回答问题时,请始终在回答中包含解决方案的关键部分。您的答案可能在此页面上存在多年,但链接可能不在。因此,有相同问题的其他人可能会找到您的帖子,但由于链接已损坏,因此无法对答案做任何事情。感谢您提供答案,我现在会投赞成票而不是投反对票。

以上是关于我想在更改主下拉菜单时构建更新表单然后更新第二个下拉列表的主要内容,如果未能解决你的问题,请参考以下文章

使用数据库结果更新 django 选择字段

单击第二个下拉列表时,黑框应保持可见

表单提交时更改的值(下拉表单)

第一个下拉菜单更改第二个下拉菜单相关项目显示

如何在 Shiny 中更改回归模型表单下拉菜单中的预测变量?

为啥我的第二个下拉菜单是复制我的第一个下拉菜单的更改操作