[問題] 請問jquery的cascade選單應用

看板Ajax作者 (中仔)時間17年前 (2008/09/22 22:39), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/7 (看更多)
請教一個JQUERY問題,我是要利用jquery cascade 做下拉選單,我參考以下的教學 http://blog.roodo.com/taikobo0/archives/6166625.html#comment-17328949 把它改成我的資料庫型式.首先我引入JS檔案,我有下回來並放在jquery資料夾中 test.php <script type="text/javascript" src="jquery/jquery.js"></script> <script type="text/javascript" src="jquery/jquery.cascade.js"></script> <script type="text/javascript" src="jquery/jquery.cascade.ext.js"></script> <script type="text/javascript" src="jquery/jquery.templating.js"></script> 下拉欄位程式碼為 <div> <p>第一項 <select id="myParentSelect"> <option value="">請選擇</option> <?php // 資料庫設定,連到我的mysql資料庫並拉出資料表 $host_sql = "localhost"; $username_sql = "root"; $password_sql = "XXX"; $link = mysql_connect($host_sql, $username_sql, $password_sql) or die("無 法連結資料庫"); mysql_select_db('mio', $link); $query = "SELECT DISTINCT class1 FROM company"; $result = mysql_query($query, $link); while ($row = mysql_fetch_assoc($result)) { echo '<option value="' . $row["class1"] . '">' . $row["class1"] . '</option>' . "\n"; }?> </select> 第二項 <select id="myFirstChildSelect"> <option value="">請選擇</option> </select> </div> 然後引入JS <script type="text/javascript"> $(function () { // 第一階層對應第二階層 $('#myFirstChildSelect').cascade('#myParentSelect', { ajax: { type: "post", url: 'action.php', data: { act: 'first', val: $('#myParentSelect').val() }, }, template: function(item) { return "<option value='" + item.Value + "'>" + item.Text + "</option>"; }, match: function(selectedValue) { return this.When == selectedValue; } }); </script> action.php部分 <?php // 資料庫設定並拉出子階層選單的資料表 $host_sql = "localhost"; $username_sql = "root"; $password_sql = "XXX"; $link = mysql_connect($host_sql, $username_sql, $password_sql) or die("無法連 結資料庫"); mysql_select_db('mio', $link); if (!empty($_POST['act'])) { $action = $_POST['act']; } if (!empty($_POST['val'])) { $parentId = $_POST['val']; } list = '['; switch ($action) { case 'first': $query = "SELECT C_name FROM company where class1= $parentId"; $result = mysql_query($query, $link); while ($row = mysql_fetch_assoc($result)) { $list .= '{\'When\':\'' . $parentId . '\',\'Value\':\'' . $row["C_name"] . '\',\'Text\':\'' . $row["C_name"] . '\'},'; } break;} $list .= ']'; echo $list; ?> 只是這樣test.php中的下拉選單還是只有第一層有資料....第二層還是空空的沒選項 滿好奇那個教學的...那個$list怎麼傳給test.php用? 有高手知道哪邊需要修正嗎?可否指點一下.謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.132.218.94 ※ 編輯: chrismaggie 來自: 220.132.218.94 (09/22 22:39)
文章代碼(AID): #18rwuB6W (Ajax)
討論串 (同標題文章)
文章代碼(AID): #18rwuB6W (Ajax)