jQuery动态主/子类别选择器

时间:2018-05-16 19:23:42

标签: javascript jquery html

  • 首先 - 我正在尝试为.container的每个实例单独运行子类post-item函数,但是你可以 请参阅,只有第一个实例正在处理子类别。
  • 第二次 - 我希望在sub-item项下设置menu之一,并在切换时显示其子类别项列表。

现在,我将预先选定的项menu设置为属于主要类别的"comb"的子类别cat1

<label class="sub-btn" for="comb">
<input class="sub-input" id="comb" type="radio" name="radios" />
</label>

我无法弄清楚如何将它连接回主类别并使其功能就像它是一个选择。我们的想法是在每个实例的sub-item上显示预先选定的menu,主要类别会自动设置为反映这一点以及另一个type-list sub-items 1}}在同一类别中可以在初始加载时切换(就像你进行主要类别选择时一样)。

https://codepen.io/moofawsaw/pen/rvdqgv

1 个答案:

答案 0 :(得分:4)

问题主要在于:

String url0 = "http://172.17.100.2/sendapp.php?co="+id_s;

更具体地说,$(".container").on("change", ".sub-btn", function() { var t = $(this).closest(".sub-btn"); $(".menu-text").text(t.find("span").text()); var pathA = t.find("#patha").attr("d"); $(".pathainitial").attr("d", pathA); var pathAc = t.find("#patha").attr("fill"); $(".pathainitial").attr("fill", pathAc); }); $(".menu-text")包含所有匹配元素,无论其容器如何。

<强>更新

这是更新的JS代码(不是整个代码,只是针对问题的第一点和第二点):

$(".pathainitial")

演示: https://codepen.io/anon/pen/xjNLdb

备注

对于每个// Switches the clicked item in `.type-list` with the active item in `.menu`. $(".type-list").on("click", ".sub-input", function() { var t = $(this).closest(".sub-btn"), // the currently clicked item m = t.closest('.type-list').prev('.menu'), // the currently active item i, i2, tt, mt, tdiv, mdiv, tcat, io; // For the currently clicked item. i = t.find('path:first-child'); tt = t.find('.sub-icon > b'); tdiv = t.parent('.sub-list'); tcat = tdiv.attr('data-for'); // For the currently active item. i2 = m.find('path:first-child'); mt = m.find('.sub-icon > b'); mdiv = m.children('.sub-item'); // Save the attributes or text before we modify them. io = { d: i2.attr('d'), fill: i2.attr('fill'), text: mt.text(), cat: mdiv.attr('data-for') }; mdiv.attr('data-for', tcat); mt.text(tt.text()); i2.attr("d", i.attr('d')) .attr("fill", i.attr('fill')); tdiv.attr('data-for', io.cat); tt.text(io.text); i.attr('d', io.d) .attr('fill', io.fill); // Put the item in its own category. if (tcat !== io.cat) { tdiv.removeClass('active'); tdiv.closest('.sub-item') .removeClass(tcat) .addClass(io.cat); } }); ,HTML应为:

.sub-icon

即。将<span class="sub-icon"> <svg ...></svg><br> <b>Text here</b> </span> 包装在Text here元素中。

b 中的.sub-item 应该有一个.menu,其值为活动的类别键/ {{1 (例如data-for):

class

预选类别cat1 中,<div class="sub-item edit current" data-for="cat1"> ... </div> 的{​​{1}}应该.cat-dropdown-menu 1}}; e.g:

.cat-item