jquery - 从 json 中获取值并放入下拉列表
问题描述
表单.php
数组(gettext('网关名称'),数组(“名称”=>“网关ID”,“类”=>“网关ID”,“ID”=>“网关ID”),“选择”,“”,“修剪| required|xss_clean', 'tOOL TIP', '请先选择网关', 'id', 'name', 'gateways', 'build_dropdown', 'where_arr', array( "status" => "0" ) ),
视图.php
$(".gateway_routing_type").change(function(){ var gateway_routing_type =$('.gateway_routing_type option:selected').val();
$.ajax({
type:'POST',
url: "<?= base_url() ?>/trunk/trunk_sipdevicelist/",
data:"gateway_routing_type="+gateway_routing_type,
success: function(response) {
var tmp = jQuery.parseJSON(response);
console.log(response);
$("#gateway_id").empty();
$.each(tmp, function(key, value) {
$("#gateway_id").append($('<option></option>').val(value.id).html(value.name));
});
$(".selectpicker").selectpicker('refresh');
}
});
});
控制器.php
$add_array = $this->input->post(); $gateway_routing_type = $add_array['gateway_routing_type'];
$accountinfo = $this->session->userdata("accountinfo");
$gateway_array = array();
if($gateway_routing_type == "0"){
$gateway_result = $this->db->get_where('gateways', array(
"gateway_routing_type" => $gateway_routing_type,
"status" => 0
));
if ($gateway_result->num_rows() > 0) {
$gateway_result = $gateway_result->result_array();
foreach ($gateway_result as $key => $value) {
$gateway_array[] = array(
"id"=>$value['id'],
"name" => $value['name']
);
}
}
}else{
$this->db->select('GROUP_CONCAT(id) as provider_id');
$provider_id = (array)$this->db->get_where("accounts",array( "status"=>0,"deleted"=>0,"type"=>3 ))->first_row();
$array = explode("," ,$provider_id['provider_id']);
foreach($array as $value){
$sipdevice_result = $this->db->get_where("sip_devices",array("accountid"=>$value));
if ($sipdevice_result->num_rows() > 0) {
$sipdevice_result = $sipdevice_result->result_array();
foreach ($sipdevice_result as $key => $value) {
$gateway_array []= array(
"id"=>$value['id'],
"name" => $value['username']
);
}
}
}
}
当ajax调用它发生然后它开始运行然后它显示选定的下拉值
解决方案
推荐阅读
- jquery - 如何动态地将多个条件附加到 laravel 查询?
- ios - 如何在 iOS 上导入带有密钥库和密码的 eth 帐户?
- db2 - 查找 ERROR [22001] [IBM][AS] 变量或参数 *N 上的转换错误的原因
- php - 基于 WooCommerce 页面的不同消息
- python - 从带有参数的终端运行 python 代码
- python - 如果语句基于 Python 中的部分字符串
- vba - 我无法让 Excel 在 VBA 宏中创建新工作表
- azure - 哪种类型的 Azure 资源可以运行 powershell 并具有系统本地的文件系统或存储?
- javascript - 调整材质-UI
导致光标退出其容器 - unity3d - 根据第一个设置第二个游戏对象的位置和旋转