使用ajax值设置bootstrap选择选项

问题描述 投票:0回答:2

我尝试用ajax在bootstrap select选项上显示数据,但是在运行数据之后不希望出现什么问题?

function get_value() {
    $id = 5;
    $q = $this->db->select('*')
                      ->from('tbl_v')
                      ->where('id', $id)
                      ->order_by('id', 'ASC')
                      ->get();

    $result = $q->result();
    echo json_encode($result );


}   

$(".change").change(function(){
    var value=$(this).val();
    if(value>0){
        $.ajax({
            url: base_url+"get_value",  
            type:"POST",
            dataType: 'json',
            cache: false,               
            data:{id:value},
            success: function(respond){
                $(".get-change").html(respond).selectpicker('refresh');
            }
        })
    }
});
ajax twitter-bootstrap codeigniter
2个回答
0
投票

试试这个...

function OnChangeValue()
{
    var pdrp = document.getElementById("yourControlIDHere");
    getvalue= pdrp.options[pdrp.selectedIndex].value;
    alert(getvalue);
}

Control

<select id="yourControlIDHere" onchange="OnChangeValue(this)" name="companyname" style="width:100%">

0
投票

Jquery代码

$(".change").change(function(){
var value=$(this).val();
if(value>0){
    $.ajax({
        url: base_url+"get_value",  
        type:"POST",
        cache: false,               
        data:{id:value},
        success: function(respond){
            $(".get-change").html(respond);
        }
    })
}
});

模态代码:

function get_records() {
    $id = 5;
    $q = $this->db->select('*')
                      ->from('tbl_v')
                      ->where('id', $id)
                      ->order_by('id', 'ASC')
                      ->get();

    $result = $q->result();
}   

控制器代码:

function get_value(){
   $ido = $this->input->post('id');
   $data = $this->your-model-name->get_records($ido);
   $option = "";
   foreach($data as $row){
      $option .= "<option value='".$value['ID']."'>".$row[0]->name."</option>";
   }
   echo $option;
}

您不必使用JSON编码。记录将直接附加到选择框。希望这可以帮到你。

© www.soinside.com 2019 - 2024. All rights reserved.