easyui combobox 动态加载的两种方法

reload 方法 

javascript代码

//指定id 和 text 否则始终选择第一个 $('#contact_city').combobox({ valueField:'id', textField:'text' }); $('#contact_province').combobox({ onSelect: function (data) {var url = "Ajax-index.php?module=Contact&action=Ajax_Contact_Province_Change"+"&contact_province=" +data.text; $('#contact_city').combobox('reload', url); } });
php代码 

$province
=trim($_GET['contact_province']); $select="SELECT p.province,c.id,c.city,p.provinceid from province as p JOIN city as c ON p.provinceid = c.provinceid where p.province='" . $province . "'";
     $result=$db->query($select); $Select_Option=""; while($row=$db->fetch_array($result)){ $key=$row[1]; $value=$row[2]; if($Select_Option==""){ $Select_Option= '{"id":"'.$key.'","text":"'.$value.'"}' ; }else{ $Select_Option=$Select_Option .",". '{"id":"'.$key.'","text":"'.$value.'"}' ; } } $Select_Option="[" . $Select_Option ."]"; echo $Select_Option;

 

loadData 方法

javascript 

$(function(){ $('#contact_city').combobox({ valueField:'id', textField:'text', editable:false //是否可编辑 }); }) $(document).ready(function () { $("#contact_province").combobox({ onChange: function (newVal,oldVal) { var contact_province=newVal; $.ajax({ type: "POST", url: "Ajax-index.php?module=<{$module_name}>&action=Ajax_Contact_Province_Change", dataType: "json", data: {"contact_province":contact_province }, beforeSend: function(){ //$('<div id="msg" />').addClass("loading").html("加载中...").css("color","#999").appendTo('.sub1'); }, success: function(json){ if(json.success==1){ //alert(json.msg); var data = $.parseJSON(json.msg); $("#contact_city").combobox("loadData", data); $("#contact_city ").combobox('select',data[0].text);//默认选中第一项 }else{ $.messager.alert('消息','数据加载失败!','error'); return false; } } }); } }); });

PHP代码 

$province
=trim($_POST['contact_province']); $select="SELECT p.province,c.city,p.provinceid from province as p JOIN city as c ON p.provinceid = c.provinceid where p.province='" . $province . "'"; $result=$db->query($select); $Select_Option=""; while($row=$db->fetch_array($result)){ $value=$row[1]; if($Select_Option==""){ $Select_Option= '{"id":"'.$value.'","text":"'.$value.'"}' ; }else{ $Select_Option=$Select_Option .",". '{"id":"'.$value.'","text":"'.$value.'"}' ; } } $Select_Option="[" . $Select_Option ."]"; $arr['success'] = 1; $arr['msg'] = $Select_Option; echo json_encode($arr);

 

posted @ 2017-12-26 18:22  海乐学习  阅读(20868)  评论(0编辑  收藏  举报