我一直试图建立一个下拉菜单,但我没有得到我想要的结果。这是我的代码:
<?php require_once 'core/init.php'?>
<?php
$sql = 'SELECT * FROM categories WHERE parent = 0';
$pquery = mysqli_query($db,$sql);
?>
<?php while($parent = mysqli_fetch_assoc($pquery)):?>
<?php
$parent_id = $parent['id'];
$sql2 = 'SELECT * FROM categories WHERE parent = "parent_id"';
$cquery = mysqli_query($db,$sql2);
?>
<li class='dropdown'>
<a href='#' class='dropdown-toggle' data-toggle='dropdown'>
<?php echo $parent['id'];?><span class='caret'</span</a>
<ul class='dropdown-menu' role='menu'>
<?php while($child = mysqli_fetch_assoc($cquery)):>
<li><a href='#'><?php echo $child['parent'];?></a>
</li>
<?php endwhile; ?>
</ul>
</li>
<?php endwhile;?>
答案 0 :(得分:-1)
在页面中编写代码之前尝试使用控制台phpmyadmin。
我认为您的代码有误。
您的代码必须像这样
$(function() {
var createChildDropdown = function(i) {
var $childDropdown = $('<div />', {
'class': 'childs'
});
$childDropdown.append($('<label />', {
'for': 'childDropdown-' + i
}).text('Child age ' + i));
$childDropdown.append($('<select />', {
'name': 'childDropdown'
}));
var options = [' 1 years old', '2 years old', '3 years old'];
options.forEach(function(option, index) {
$childDropdown.find('select').append($('<option />').text(option).attr('value', `,${index}`));
});
return $childDropdown;
};
var destroyChildDropdown = function($el, i) {
$el.find('div.childs').get(i).remove();
};
$(".button-click-child a").on("click", function() {
var button = $(this);
var oldVal = parseInt(button.closest("ul").prev().val());
var newVal = (button.text() == "+") ? oldVal + 1 : (oldVal > 0) ? oldVal - 1 : 0;
var total_value = "";
if(newVal >= 5) return;
button.closest("ul").prev().val(newVal);
$(".cat_textbox").each(function() {
var cat = $(this).prev('span').text();
});
if (oldVal < newVal) {
$('.childDropdowns').append(createChildDropdown(newVal));
} else if (oldVal > newVal) {
destroyChildDropdown($('.childDropdowns'), newVal);
}
})
})
我建议你使用ajax再次美丽。