-1

我需要先选择source_type,然后它会location根据source_type

查看文件

    <select name="source_type" id="source_type">
       <option value="">Source Type</option>
       <option value="Internal">Internal</option>
       <option value="External">External</option>
    </select>

    <select name="location" id="location">
       <option value="">Select</option>
    </select>

    $('#source_type').change(function(){
        $.ajax({
            type: 'post',
            data: 'source_type'+$(this).val(),
            url: 'get_sources_location',
            success: function(data) {
                $('#source_location').val(data);
            }
        });
    });

..控制器

    public function get_sources_location()
    {
        $source_type = $this->input->post('source_type');

        $this->load->model('documents_model');
        $results = $this->documents_model->get_locations();
        echo $results;
    }

..模型

    public function get_locations()
    {
     $query = $this
                ->db
                ->select('location')
                ->where('classification', $source_type = $this->input->post('source_type'))
                ->get('sources_location');

     $data = array();

     foreach ($query->result() as $row)
     {
         $data = $row->location;
     }
     return $data;
   }
4

3 回答 3

2

尝试这样的事情

代码

<select name="source_type" id="source_type" onchange="populateLocation(this.value)">
   <option value="">Source Type</option>
   <option value="Internal">Internal</option>
   <option value="External">External</option>
</select>

<select name="location" id="location">
   <option value="">Select</option>
</select>

使用 JQUERY 的 JAVASCRIPT 代码

function populateLocation(source_type_value){
            if(source_type_value !=''){
                /*
                    Pass your source type value to controller function which will return you json for your
                    loaction dropdown
                */
                var url = 'controller/function/'+ source_type_value; // 
                $.get(url, function(data) {
                 var $select = $('location');
                 // removing all previously filled option
                 $select.empty();
                 for (var propt in data) {
                        $select.append($('<option />', {
                            value: propt,
                            text: data[propt]
                        }));
                 }

            }
        }

从控制器返回的JSON 格式

{"":"Select","1":"New york","2":"London","3":"Mumbai"}
于 2013-02-25T06:52:13.337 回答
1

像这样放置一个 AJAX 函数,

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

  $.ajax({
     type : 'POST',
     data : 'source_id='+ $(this).val(),
     url : 'controller/method',
     success : function(data){
                 $('#location').val(data);
     }
 });
});

在控制器中,放置一个获取值的方法,

public function getLocations() {

     $source_id = $this->input->post('source_id');

     // Call the model function and get all the locations based on the source type id

     // Populate the dropdown options here

     echo $result;
}
于 2013-02-25T06:46:24.180 回答
0

change事件处理程序附加到您的第一个 DD 并向服务器发送 ajax 调用以获取数据。在 ajax 调用的成功回调中填充第二个 DD。下面是一个简单的概念证明

$("#source_type").change(function(e)
{
    $.ajax({
        url:'/echo/json',
        success:function(data){
            console.log("success");
            $("<option/>",{text:'google',value:'google'}).appendTo("#location");
        }
    });

演示

于 2013-02-25T06:42:55.487 回答