使用js+jquery實(shí)現(xiàn)無限極聯(lián)動(dòng)
今天工作需要寫樹形的聯(lián)動(dòng),于是寫了個(gè)可擴(kuò)展的無限極聯(lián)動(dòng)下拉選項(xiàng)
代碼寫的比較凌亂 先mark有空再整理
隨便截個(gè)圖!
先貼數(shù)據(jù)庫
id | category_name 分類名 | pid 父分類id | orders 排序 |
---|---|---|---|
1 | 22223331 | 0 | 1 |
2 | 2222111 | 1 | 1 |
12 | 44444 | 11 | 0 |
5 | 2222 | 1 | 1 |
6 | 2222 | 1 | 1 |
11 | 333 | 2 | 0 |
13 | 555555 | 12 | 0 |
頁面代碼 用的SMARTY
<div id="select" >
<select name="category_1" id="category_1" onChange="change('category_1');">
<option>請選擇分類</option>
<!-- {foreach from=$galleryCategory item=category} -->
<option value="{$category.id}">{$category.category_name}</option>
<!-- {/foreach} -->
</select>
</div>
$galleryCategory 去數(shù)據(jù)的PHP代碼為
$sql = " select * from yl_gallery_category where pid = 0";
$galleryCategory = $db->query($sql);
$smarty->assign("galleryCategory",$galleryCategory);
用的原生態(tài)代碼 還是比較容易理解的
然后就是關(guān)鍵的 JS代碼了function change(val) {
var str = val; //select的id
var num; //當(dāng)前級數(shù)
var id; // 分類id
num = str.substr(9,10);
//alert(num);
var nownum = parseInt(num)+1; // 將字符串轉(zhuǎn)換為數(shù)字
id = $("#"+str+"").val();
var r = /^[1-9]+[0-9]*]*$/; //正整數(shù)
if (!r.test(id)) {
//清空過時(shí)的選項(xiàng)
$("select").each(function(index){
if(index+1 > num) {
$(this).remove();
}
})
return false;
}
var url = 'gallery.php?act=category&pid='+id;
$.ajax({
type: "POST",
cache: false,
url: url,
datatype : 'json',
timeout : 3000,
success: function(result){
if ( result != 0) {
var html = "<select name=category_"+nownum+" id=category_"+nownum+" onChange=change('category_"+nownum+"'); >";
html += "<option>請選擇分類 </option>";
var datas = eval(result);
$.each(datas, function(i,val){
html += "<option value='"+val.id+"' >"+val.category_name+"</option>";
});
html += "</select>";
//清空過時(shí)的選項(xiàng)
$("select").each(function(index){
if(index+1 > num) {
$(this).remove();
}
})
$("#select").append(html);
} else {
//清空過時(shí)的選項(xiàng)
$("select").each(function(index){
if(index+1 > num) {
$(this).remove();
}
}) }
},
error: false
});
}
AJAX 取數(shù)據(jù)的PHP代碼
$sql = " select * from yl_gallery_category where pid = " .$pid;
$res = $db->query($sql);
if (empty($res)) {
$res = 0;
}
echo json_encode($res);
OK 大功告成!
相關(guān)文章
jQuery自定義動(dòng)畫函數(shù)實(shí)例詳解(附demo源碼)
這篇文章主要介紹了jQuery自定義動(dòng)畫函數(shù)實(shí)現(xiàn)方法,形式實(shí)例分析了jQuery通過插件結(jié)合數(shù)學(xué)運(yùn)算實(shí)現(xiàn)滑塊動(dòng)畫運(yùn)動(dòng)的效果,并附完整demo源碼供讀者下載,需要的朋友可以參考下2015-12-12基于jQuery實(shí)現(xiàn)淡入淡出效果輪播圖
這篇文章主要為大家詳細(xì)介紹了基于jQuery淡入淡出效果輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08基于jQuery實(shí)現(xiàn)彈出可關(guān)閉遮罩提示框?qū)嵗a
這篇文章主要介紹了基于jQuery實(shí)現(xiàn)彈出可關(guān)閉遮罩提示框?qū)嵗a的相關(guān)資料2016-07-07jquery實(shí)現(xiàn)簡單的banner輪播效果【實(shí)例】
下面小編就為大家?guī)硪黄猨query實(shí)現(xiàn)簡單的banner輪播效果【實(shí)例】。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-03-03Jquery獲取radio選中值實(shí)例總結(jié)
在本篇文章里小編給大家分享了關(guān)于Jquery獲取radio選中值實(shí)例總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2019-01-01jQuery 位置函數(shù)offset,innerWidth,innerHeight,outerWidth,outerHei
jQuery的位置函數(shù)(offset(),innerWidth(),innerHeight(),outerWidth(),outerHeight(),scrollTop(),scrollLeft())小應(yīng)用2010-03-03