今天工作需要 要寫樹形的聯動,於是寫了個可擴充的無限極聯動下拉選項
代碼寫的比較淩亂 先mark 有空再整理
操蛋!
公司連QQ都不能上 隨便截個圖!
先貼資料庫
| 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
請選擇分類 {$category.category_name}
$galleryCategory 去資料的PHP代碼為
$sql = " select * from yl_gallery_category where pid = 0";$galleryCategory = $db->query($sql);$smarty->assign("galleryCategory",$galleryCategory);
給辭職的同事的項目擦屁股 用的原生態代碼 還是比較容易理解的
然後就是關鍵的 JS代碼了 change(val) {
str = val; num; id; num = str.substr(9,10 nownum = parseInt(num)+1; id = $("#"+str+"" r = /^[1-9]+[0-9]*]*$/; (! $("select").each((index+1 > url = 'gallery.php?act=category&pid='+"POST"'json'3000 ( result != 0 html = ""+= "請選擇分類 " datas =+= ""+val.category_name+""+= "" $("select").each((index+1 >"#select"
//清空過時的選項 $("select").each(function(index){ if(index+1 > num) { $(this).remove(); } })
AJAX 取資料的PHP代碼
$sql = " select * from yl_gallery_category where pid = " .$pid;$res = $db->query($sql);if (empty($res)) {$res = 0;}echo json_encode($res);
OK 大功告成!
http://www.bkjia.com/PHPjc/440345.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/440345.htmlTechArticle今天工作需要 要寫樹形的聯動,於是寫了個可擴充的無限極聯動下拉選項 代碼寫的比較淩亂 先mark 有空再整理 操蛋! 公司連QQ都不能上...