【问题标题】:dynamic select option in codeignitercodeigniter 中的动态选择选项
【发布时间】:2017-06-12 08:13:37
【问题描述】:

我正在 codeigniter 中创建可靠的选择选项。填充第一个选项很容易,但我在显示第二个选项和第三个选项时有点麻烦。需要这方面的帮助

我的数据库包含:

在头表中: h_id,头字段, 副标题表 h_id、sh_id、副标题字段和 积分表 sh_id、points_name 字段


控制器

class Mobile extends App_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->helper(array('url','language','form'));
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<p class="text-red">', '</p>');
        $this->lang->load('auth');
        $this->load->model('mobiles_model'); 

    }
    function index()
    {
        if (!$this->data['logged_in']) 
        {
            redirect('auth/login','refresh');
        }

        $rows = $this->mobiles_model->get_users_devices();
        if ($rows) 
        {
            $this->data['devices_list'] = $rows;    
         }
        $this->_render_page('trackall', $this->data);       
    }

function _render_page($view, $data=null, $render=false)
    {

        $this->viewdata = (empty($data)) ? $this->data: $data;

        $view_html = $this->load->view($view, $this->viewdata, $render);

        if (!$render) return $view_html;
    }
}

模态

public function get_users_devices()
            {   
                $this->db->select('A.h_id as id,A.head');
                $this->db->from($this->table_head.' as A'); 
                $query = $this->db->get();
            if ($query->num_rows() > 0) {
                return $query->result_array();
            }
            return FALSE;
            }

查看

<select id="head" name="head" class="form-control input-sm" onchange = "calljavascriptfunction();">
        <option value="default">--Select--</option>
        <?php if(isset($devices_list))
        foreach ($devices_list as $value) { ?>
          <option value="<?php echo $value['h_id'] ?>"><?php echo $value['head'] ?></option>
         <?php } ?>                                   
      </select>


      <select id="subhead" name="subhead" class="form-control input-sm">
        <option value="default">--Select--</option>    


      </select>

 <select id="points" name="points" class="form-control input-sm">
        <option value="default">--Select--</option>    


      </select>

我必须根据我选择的内容填充另一个。我尝试了几种方法,但都没有奏效。 感谢您的帮助,我真的陷入了困境。

【问题讨论】:

  • 我认为您需要在依赖下拉列表中加载数据的完整代码。
  • 是的,我认为只是停留在 ajax 调用和 ci
  • stackoverflow.com/questions/27659934/… 检查此问题及其解决方案。
  • 已经试过了,谢谢
  • 检查我的答案

标签: javascript php codeigniter drop-down-menu


【解决方案1】:

假设您在 HTML 中加载了以下视图,并且您需要根据第一个下拉列表 #head 获取值。

查看代码:

<select id="head">
    <option id="1"> Mobile </option>
    <option id="2"> Tabs </option>
    <option id="3"> Laptops </option>
</select>

<select id="subhead">
</select>

jquery ajax

$("#head").change(function(){

    var post_url = '<?php echo base_url() ?>mobile/getByDeviceId'
    $.ajax({
        type: "POST",
        url: post_url,
        data : { "hid" : $(this).val() },
        success: function(response){
            $.each(response, function(value, key) {
                $("#subhead").append("<option id="+value.modelId+">"+value.modelName+"</option>");
            })
        }
    });
});

控制器功能:

function getByDeviceId(){

    $hid = $this->input->post("hid");

    $modelList = $this->mobile_model->getByDeviceId($hid);

    echo(json_encode($modelList));
}

模型功能:

function getByDeviceId($hid){

    $this->db->select("modelId, modelName");
    $this->db->from("models");
    $this->db->where("hid", $hid);
    $query = $this->db->get();

    return $query->result();
}

【讨论】:

  • 请成功console.log(response);,并检查您是如何接收来自服务器的响应的。您需要相应地修改代码。
【解决方案2】:

使用 jquery ajax() 来执行此操作,因为 ajax() 不会干扰页面的当前状态并从服务器获取数据,您可以将其附加到下拉列表中。

ajax() 语法:

$.ajax({
        url: 'process.php',                        
        type: 'POST',                                  
        data: {
            var1  :val1,
            var2  :val2
        },
        success: function(response){    // response from process
            // do your stuff here
        }
    });

【讨论】:

  • $("#head").change(function() { var h_id = {"h_id" : $('#head').val()}; $.ajax({ type: “POST”,数据:h_id,url:“mobile/ajax_get”,成功:function(data) { $.each(data, function(i, data) { $('#subhead ').append(""); }); } }); });
  • data : { "hId" : h_id } 你需要发送这样的值。
  • 数据应该像data: { var1 :val1, var2 :val2 },
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-13
  • 1970-01-01
  • 2014-12-23
  • 1970-01-01
  • 1970-01-01
  • 2018-08-17
相关资源
最近更新 更多