因此,我有带有类别和子类别的下拉垂直菜单。它已正确填充,但是单击并选择一些子类别时出现问题。它什么都不会发生。只是下拉列表已关闭,不会加载新页面。这是cat.php
function category_list($category_parent_id = 0)
{
// build our category list only once
static $cats;
if (!is_array($cats)) {
$sql = 'SELECT * FROM cat';
$res = mysql_query($sql);
$cats = array();
while ($cat = mysql_fetch_assoc($res)) {
$cats[] = $cat;
}
}
// populate a list items array
$list_items = array();
foreach($cats as $cat) {
// if not a match, move on
if (( int )$cat['parentid'] !== ( int )$category_parent_id) {
continue;
}
// open the list item
$list_items[] = '<li>';
// construct the category link
$list_items[] = '<a href="product.php?id=' . $cat['id'] . '">';
$list_items[] = $cat['name'];
$list_items[] = '</a>';
// recurse into the child list
$list_items[] = category_list($cat['id']);
// close the list item
$list_items[] = '</li>';
}
// convert to a string
$list_items = implode('', $list_items);
// if empty, no list items!
if ('' == trim($list_items)) {
return '';
}
// ...otherwise, return the list
return '<ul id="nav-cat">' . $list_items . '</ul>';
}
这是下拉脚本
$(document).ready(function () {
$('#nav-cat > li > a').click(function (e) {
$('#nav-cat li ul').slideUp();
if ($(this).attr('class') != 'active') {
$('#nav-cat li a').removeClass('active');
$(this).next().slideToggle();
$(this).addClass('active');
} else {
$('#nav-cat li a').removeClass('active');
}
e.preventDefault();
});
});
当我单击链接时,$list_items[] = '<a href="product.php?id=' . $cat['id'] . '">';
没有任何反应。这就是我传递product.php
的方式id
。我知道有点混乱...
if(isset($_GET['id']) && is_numeric($_GET['id'])){
$id = $_GET['id'];
$query = "SELECT * FROM products WHERE cat = '" . $_GET['id']."'";
$result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
$line = mysqli_fetch_array($result, MYSQL_BOTH);
if (!$line) echo '';
$previd = -1;
$currid = $line[0];
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$previous_ids = array();
do {
$previous_ids[] = $line[0];
$currid = $line[0];
if ($currid == (int)$_GET['id']) break;
$previd = end($previous_ids);
$line = mysqli_fetch_array($result, MYSQL_BOTH);
} while ($line);
}
if ($line) {
...
我真的不知道问题出在哪里。有任何想法吗?如果需要,我可以发布更多代码。
如JQuery API上的preventDefault所述,它说“单击的锚点不会将浏览器带到新的URL”。如果您尝试加载内容而不刷新/离开页面,请考虑使用AJAX。
附带说明:您的递归php函数会创建许多<ul>
具有相同ID的。考虑改用类。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句