使用ajax值设置引导选定选项
问题描述:
我尝试使用ajax在引导选择选项上显示数据,但运行数据后不想出现什么问题?使用ajax值设置引导选定选项
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');
}
})
}
});
答
尝试......
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
我知道你使用的是PHP,但它有点类似。 –
+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编码。记录将直接附加到选择框。希望这可以帮到你。
尝试使用此代码查看值Alert(JSON.Stringify($(this).val()))。 –
不要忘了'json_encode()'你的'$ q-> result()' –
对不起,我正在更新我的问题, –