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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM