jQuery結(jié)合PHP+MySQL實(shí)現(xiàn)二級聯(lián)動下拉列表[實(shí)例]

實(shí)現(xiàn)原理:根據(jù)省份值的變動,通過jQuery把sf_id傳給后臺php文件處理,php通過查詢MySQl數(shù)據(jù)庫,得到對應(yīng)的地市名,并返回JSON數(shù)據(jù)給前端處理,即實(shí)現(xiàn)聯(lián)動效果!
為便于講解,這里直接給出省份:河南省(sf_id=1) 浙江省(sf_id=2),而地市和學(xué)生信息則分別建立兩張數(shù)據(jù)表!編碼方式均為:utf8!新建數(shù)據(jù)庫并執(zhí)行以下SQL語句!
/* 地市表 */
create TABLE IF NOT EXISTS `dishi`(
`ds_id` int(3) auto_increment not null primary key,
`sf_id` int(3) not null default '0',
`ds_name` varchar(50) not null
);
/* 學(xué)生表 */
create TABLE IF NOT EXISTS `xuesheng`(
`xs_id` int(3) auto_increment not null primary key,
`ds_id` int(3) not null default '0',
`xs_name` varchar(50) not null
);接著搭個(gè)前臺架子:
<table border="0" width="100%">
<tr>
<td width=100>省份</td>
<td>
<select name="sf_id" id="sf_id" title="選擇省份">
<option value="1">河南省</option>
<option value="2">浙江省</option>
</select>
</td>
</tr>
<tr>
<td>地市</td>
<td>
<select name="ds_id" id="ds_id" title="選擇地市">
</select>
</td>
</tr>
<tr>
<td>學(xué)生姓名</td>
<td><input type=text maxlength=20 name="xs_name" value=""></td></tr>
</table>
接著就是jQuery部分,詳解可看代碼后注釋部分:
<script language="JavaScript">
function getVal(){
$.getJSON("select.php",{sf_id:$("#sf_id").val()},function(json){
var ds_id = $("#ds_id");
$("option",ds_id).remove(); //清空原有的選項(xiàng),也可使用 ds_id.empty();
$.each(json,function(index,array){
var option = "<option value='"+array['ds_id']+"'>"+array['ds_name']+"</option>";
ds_id.append(option);
});
});
}
//下面是頁面加載時(shí)自動執(zhí)行一次getVal()函數(shù)
$().ready(function(){
getVal();
$("#sf_id").change(function(){//省份部分有變動時(shí),執(zhí)行g(shù)etVal()函數(shù)
getVal();
});
});
</script>
其中的select.php就是關(guān)鍵部分了,此文件接收前臺通過$.getJSON方法傳遞過來的參數(shù) sf_id,然后select.php根據(jù)省份sf_id獲取對應(yīng)的地市數(shù)據(jù),再返回JSON數(shù)據(jù),前臺通過jQuery將JSON數(shù)據(jù)進(jìn)行處理,寫入<option>,即完成了聯(lián)動效果!
$sf_id = $_GET["sf_id"];
if(isset($sf_id)){
$q=mysql_query("select * from dishi where sf_id = $sf_id");
while($row=mysql_fetch_array($q)){
$select[] = array("ds_id"=>$row['ds_id'],"ds_name"=>urlencode($row['ds_name']));
}
echo urldecode(json_encode($select));
}
其中的urlencode()、urldecode()函數(shù)為防止中文數(shù)據(jù)庫內(nèi)容亂碼!這里還需要注意的是,select.php不得再有其它數(shù)據(jù)返回,免得JSON返回錯(cuò)誤!
最后補(bǔ)充一下,有童鞋問這效果在添加學(xué)生信息的時(shí)候能方便使用,如果有傳遞過來的學(xué)生信息需要編輯時(shí),不能直接顯示要編輯的學(xué)生所處的地市!這里就需要加個(gè)判斷了:
首先將上面的:<select name="ds_id" id="ds_id" title="選擇地市"> </select>部分做個(gè)判斷
<select name="ds_id" id="ds_id" title="選擇地市">
<?php if( isset($_GET['ds_id']) ){//返回要編輯的學(xué)生所屬的地市
$sql="SELECT * FROM dishi WHERE ds_id=".$ds_id;
$resultds=mysql_query($sql,$conn);
while($listds=mysql_fetch_array($resultds)){ ?>
<option value="<?php echo $listds['ds_id'] ?>" <?php if( $listds['ds_id']==$ds_id ){ echo 'selected="selected"'; } ?> name="ds_id"><?php echo $listds['ds_name'] ?></option>
<?php } ?>
<?php } ?>
</select>
然后在頁面加載時(shí),首次執(zhí)行g(shù)etVal()函數(shù)前做判斷,說明看注釋部分:
$().ready(function(){
//當(dāng)$ds_id不為空,表示加載修改狀態(tài)的表單,此時(shí)就不能在頁面加載時(shí)立即調(diào)用getVal()函數(shù),以避免無法顯示要修改的賬目所在的收支分類
<?php if( empty($ds_id) ){ ?>//當(dāng)$ds_id為空,表示加載添加表單,此時(shí)要在頁面加載時(shí)立即調(diào)用getVal()函數(shù),以顯示當(dāng)前收支類型的子分類
getVal();
<?php } ?>
$("#sf_id").change(function(){
getVal();
});
});
好了,差不多結(jié)束吧!
- jquery+json 通用三級聯(lián)動下拉列表
- jQuery實(shí)現(xiàn)多級聯(lián)動下拉列表查詢框
- jQuery 下拉列表 二級聯(lián)動插件分享
- jQuery ajax+PHP實(shí)現(xiàn)的級聯(lián)下拉列表框功能示例
- 基于jquery的二級聯(lián)動菜單實(shí)現(xiàn)代碼
- 簡單實(shí)用jquery版三級聯(lián)動select示例
- JQuery實(shí)現(xiàn)級聯(lián)下拉框效果實(shí)例講解
- jquery 實(shí)現(xiàn)二級/三級/多級聯(lián)動菜單的思路及代碼
- 省市區(qū)三級聯(lián)動jquery實(shí)現(xiàn)代碼
- 基于jquery的無限級聯(lián)下拉框js插件
- jQuery實(shí)現(xiàn)動態(tài)生成年月日級聯(lián)下拉列表示例
相關(guān)文章
jQuery實(shí)現(xiàn)左右滑動的toggle方法
下面小編就為大家分享一篇jQuery實(shí)現(xiàn)左右滑動的toggle方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
jQuery的cookie插件實(shí)現(xiàn)保存用戶登陸信息
保存用戶登陸信息的方法有很多,本文為大家介紹的這個(gè)方法是通過cookie插件來實(shí)現(xiàn),需要的朋友可以參考下2014-04-04
jquery應(yīng)用實(shí)例分享_實(shí)現(xiàn)手風(fēng)琴特效
下面小編就為大家分享一篇jquery應(yīng)用實(shí)例_實(shí)現(xiàn)手風(fēng)琴特效的示例代碼。具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
jquery的ajax和getJson跨域獲取json數(shù)據(jù)的實(shí)現(xiàn)方法
本篇文章主要是對jquery的ajax和getJson跨域獲取json數(shù)據(jù)的實(shí)現(xiàn)方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02
EasyUI中combobox默認(rèn)值注意事項(xiàng)
這篇文章主要介紹了EasyUI中combobox默認(rèn)值注意事項(xiàng),是個(gè)人在項(xiàng)目中遇到并解決的事宜,分享給大家,需要的朋友可以參考下2015-03-03
jQuery實(shí)現(xiàn)鼠標(biāo)劃過修改樣式的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)鼠標(biāo)劃過修改樣式的方法,涉及jQuery針對鼠標(biāo)事件及樣式操作的技巧,是jQuery鼠標(biāo)事件的典型應(yīng)用,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
jquery實(shí)現(xiàn)點(diǎn)擊展開列表同時(shí)隱藏其他列表
這篇文章主要介紹了jquery實(shí)現(xiàn)點(diǎn)擊展開列表同時(shí)隱藏其他列表的方法,涉及jquery鼠標(biāo)事件及節(jié)點(diǎn)的遍歷與屬性操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
各瀏覽器中querySelector和querySelectorAll的實(shí)現(xiàn)差異分析
querySelector和querySelectorAll的參數(shù)須是符合 css selector 的字符串。不同的是querySelector返回的是一個(gè)對象,querySelectorAll返回的一個(gè)集合(NodeList)2012-05-05

