【问题标题】:Wordpress/Gravity Forms - How to show/hide items in a single <ul> list based on a checkbox being checked?Wordpress/Gravity Forms - 如何根据选中的复选框在单个 <ul> 列表中显示/隐藏项目?
【发布时间】:2016-07-16 01:57:07
【问题描述】:

我在 Wordpress 安装中使用 Gravity Forms,并尝试在单个复选框列表中创建类似父/子的结构。为了阐明我的目标结构,请注意有 13 个父项,每个项都有大约 30 个子项。

目标是,只要选中其中一个父项旁边的复选框,就会出现该特定父项下方的约 30 个子项,以便也可以选中它们。

因此,当用户第一次加载页面时,唯一可以立即看到的项目是父项目。

这里的困难是我只能在一个无序列表中工作,我不能有多个列表。此外,我无法更改每个列表项的类,我必须使用分配给每个列表项的现有唯一类。

我一直在尝试一些 CSS/Java/JQuery 解决方案,但我想看看这里的社区认为什么是解决这个问题的最佳方法。

下面是构成列表的 HTML 示例:

<ul class="gfield_checkbox" id="input_4_67"><li class="gchoice_4_67_1">
                            <input name="input_67.1" type="checkbox" value="1160" id="choice_4_67_1" tabindex="3">
                            <label for="choice_4_67_1" id="label_4_67_1">3-D Printing</label>
                        </li><li class="gchoice_4_67_2">
                            <input name="input_67.2" type="checkbox" value="512" id="choice_4_67_2" tabindex="4">
                            <label for="choice_4_67_2" id="label_4_67_2">3-D Technology: Rapid Manufacturing/Prototyping Equipment</label>
                        </li><li class="gchoice_4_67_3">
                            <input name="input_67.3" type="checkbox" value="962" id="choice_4_67_3" tabindex="5">
                            <label for="choice_4_67_3" id="label_4_67_3">Additives: Accelerators</label>
                        </li><li class="gchoice_4_67_4">
                            <input name="input_67.4" type="checkbox" value="963" id="choice_4_67_4" tabindex="6">
                            <label for="choice_4_67_4" id="label_4_67_4">Additives: Antiblocking Agents</label>
                        </li><li class="gchoice_4_67_5">
                            <input name="input_67.5" type="checkbox" value="964" id="choice_4_67_5" tabindex="7">
                            <label for="choice_4_67_5" id="label_4_67_5">Additives: Antifogging agents</label>
                        </li>
          </ul>

我想要完成的一个例子是,当gchoice_4_67_1 旁边的复选框被选中时,gchoice_4_67_2, gchoice_4_67_3, gchoice_4_67_4, ..., gchoice_4_67_20 会出现。当复选框未选中时,这些列表项将隐藏。

我一直在玩这个代码,但它只适用于一个类。有没有办法让它适用于多个课程?也许某种数组?请原谅我对 jQuery 的不熟悉。

jQuery(function ($) {
    var checkbox = $('.gchoice_4_67_1');
    var dependent = $('.gchoice_4_67_2');
    if (checkbox.attr('checked') !== undefined){
         dependent.show();
    } else {
            dependent.hide();
    }

    checkbox.change(function(e){
         dependent.toggle();
    });
});

非常感谢您提供的任何帮助,非常感谢!

【问题讨论】:

  • 我一直在玩这个代码,但它只适用于一个类。有没有办法让它适用于多个课程?也许某种数组?请原谅我对 jQuery 的不熟悉。 jQuery(function ($) { var checkbox = $('.gchoice_4_67_1'); var dependent = $('.gchoice_4_67_2'); if (checkbox.attr('checked') !== undefined){ dependent.show(); } else { dependent.hide(); } checkbox.change(function(e){ dependent.toggle(); }); });

标签: javascript jquery html css wordpress


【解决方案1】:
jQuery(function ($) {
    var trigger = $('#choice_4_67_1');
    trigger.change(function(){
        if ( $(this).is(":checked") ) {
            $('.gfield_checkbox input[id^="choice_4_67_"]').parent().show();
        } else {
            $('.gfield_checkbox input[id^="choice_4_67_"]').parent().hide();
        }
    })

});

^= 基本上表示它将匹配任何以“choice_4_67_”开头的 id。我认为它应该可以解决您的问题。

【讨论】:

  • 非常感谢您的回答。使它与任何以“choice_4_67_”开头的 id 匹配的代码部分非常棒。但是,我注意到您的代码检查了以“choice_4_67_”开头的框,所需的行为实际上是显示/隐藏元素本身(列表项)。
  • 我刚刚编辑过。现在它将隐藏所选输入的每个“li”元素
  • 太棒了,看起来很棒,越来越近了,非常感谢您的帮助。 only 问题是,理想情况下,我试图隐藏一系列选择,其中可能不包含 all 以“choice_4_67_”开头的元素例如,我可能试图通过“choice_4_67_20”隐藏“choice_4_67_2”。有没有办法用这个代码选择一个 range ?再次感谢您的帮助!!!
  • 也许你会提供一些 jsfidle 演示,因为我不明白你想要做什么的整个结构。在这种情况下,也许我可以提出更好的解决方案。
  • 谢谢lomoboboo。 Here's 一个带有我正在使用的列表的 HTML 的 jsfiddle。我插入了您提供的代码。例如,当检查“添加剂”时,将显示“添加剂”下的“添加剂”下的 30个孩子。取消选中时,子项将被隐藏。这种相同的行为将重复整个列表,总共有 13 个父/子关系(单击父项,子项出现)。这将防止用户被选择轰炸并使列表更易于管理。希望这是可能的!
猜你喜欢
  • 2021-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-22
  • 2013-09-28
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
相关资源
最近更新 更多