【发布时间】:2021-06-15 20:22:24
【问题描述】:
我正在尝试根据类别获取我的子类别 这是我的代码
<div class="form-group">
<label>Category</label>
<select id="categoryList" class="form-control" name="category_id" required>
<?php
$categories = $this->crud_model->get_categories()->result_array();
foreach ($categories as $key => $category):?>
<option value="<?php echo $category['slug']; ?>"><?php echo $category['name']; ?></option>
<?php endforeach; ?>
</select>
<label>Subcategory</label>
<select id="subcategoryList" class="form-control" name="subcategory_id" required disabled>
<?php
$sub_categories = $this->crud_model->get_sub_categories($category['id']);
foreach ($sub_categories as $sub_category): ?>
<option id="sub_category-<?php echo $sub_category['id'];?>" name="sub_category" class="parent-<?php $selected_category_id == $sub_category['id'] ?> subcategory" value="<?php echo $sub_category['slug']; ?>"><?php echo $sub_category['name']; ?></option>
<?php endforeach; ?>
</select>
jquery
$('#categoryList').on('change', function () {
$("#subcategoryList").attr('disabled', false); //enable subcategory select
$("#subcategoryList").val("");
$(".subcategory").attr('disabled', true); //disable all category option
$(".subcategory").hide(); //hide all subcategory option
$(".parent-" + $(this).val()).attr('disabled', false); //enable subcategory of selected category/parent
$(".parent-" + $(this).val()).show();
});
但它只会显示最后一个类别的子类别 我该如何解决这个问题?
或者我之前使用过这段代码
<div class="form-group">
<label>Category</label>
<select id="categoryList" class="form-control" name="category_id" required>
<?php
$categories = $this->crud_model->get_categories()->result_array();
foreach ($categories as $key => $category):?>
<option value="<?php echo $category['slug']; ?>"><?php echo $category['name']; ?></option>
<?php endforeach; ?>
</select>
<label>Subcategory</label>
<select id="subcategoryList" class="form-control" name="subcategory_id" required disabled>
<?php
$sub_categories = $this->crud_model->get_sub_categories($category['id']);
foreach ($sub_categories as $sub_category): ?>
<option id="sub_category-<?php echo $sub_category['id'];?>" name="sub_category" class="parent-<?php $selected_category_id == $sub_category['id'] ?> subcategory" value="<?php echo $sub_category['slug']; ?>"><?php echo $sub_category['name']; ?></option>
<?php endforeach; ?>
</select>
</div>
jquery
$(function () {
$("select.dependent").each(function (i, sel) {
var original = $(this).clone(),
dependent = $("<select></select>").attr({
name: this.name
}).insertAfter(this)
this.name = "categories_" + this.name
$("optgroup", this).replaceWith(function () {
return "<option>" + this.label + "</option>"
})
$("option:first",this).prop("selected",true)
$(this).removeAttr("multiple").on("change", function () {
var cat = $(this).val()
dependent.html(original.children("[label='" + cat + "']").html())
}).change()
console.log(this)
})
})
但由于我需要 2 为子类别单独选择一个,因此我可以为其放置一个 onchange="filter(this)" 以仅基于子类别显示结果,我已将代码更改为第一部分 所以如果有一种方法可以让我在第二部分中做到这一点,也可以解决我的问题
【问题讨论】:
-
能否提供一个工作演示
-
我在 html 中没有看到任何
subcategory或parent-<val>类 -
你不能以这种方式做这些事情。我能理解的是,您有一个主要的父类别和一个子类别。您想根据选定的主类别更改/填充子类别数据吗?从这段代码看起来你正在尝试用 jQuery 做一些事情,但让我告诉你,首先你必须修复那个 php 代码。
-
<?php $selected_category_id == $sub_category['id'] ?>这不打印任何东西?如果你检查浏览器控制台,你会看到所有类都有parent- subcategory? -
您需要更改制作子类别下拉列表的方式。像这样做。再次为每个类别运行类别,然后为每个类别制作子类别
标签: javascript php jquery codeigniter