【问题标题】:Wordpress/Gravity Forms - How to show/hide items based on a radio button being checked?Wordpress/Gravity Forms - 如何根据选中的单选按钮显示/隐藏项目?
【发布时间】:2021-02-21 01:09:48
【问题描述】:

我正在尝试根据重力表单中单选按钮字段中的输入来显示/隐藏 div。我使用 jQuery here 找到了一个答案,它非常适用于复选框,但是,当我尝试使用单选按钮执行相同操作时,它会执行第一部分并在选择后隐藏 div,但不会显示 div再次取消选择单选按钮。

这是我正在使用的 jQuery:

jQuery(function ($) {
   var trigger = $('#choice_13_1_0');
   trigger.change(function(){
       if ( $(this).is(":checked") ) {
           jQuery(".conditional-display").hide();
       } else {
           jQuery(".conditional-display").show();
       }
   });
});

这是 Gravity 表单生成的 HTML:

<ul class="gfield_radio" id="input_13_1">
<li class="gchoice_13_1_0">
    <input name="input_1" type="radio" value="Option One" id="choice_13_1_0" tabindex="1">
    <label for="choice_13_1_0" id="label_13_1_0">
        Option One
    </label>
</li>
<li class="gchoice_13_1_1">
    <input name="input_1" type="radio" value="Option Two" id="choice_13_1_1" tabindex="2">
    <label for="choice_13_1_1" id="label_13_1_1">
        Option Two
    </label>
</li>
<li class="gchoice_13_1_2">
    <input name="input_1" type="radio" value="Option Three" id="choice_13_1_2" tabindex="3">
    <label for="choice_13_1_2" id="label_13_1_2">
        Option Three
    </label>
</li>

我将这个 div 放在页面的其他位置:

<div class="conditional-display">
    Content to conditionally display based on radio field selection
</div>

In this example what should happen is that the div will be hidden when option one is selected and then show again when option two or three are selected.

【问题讨论】:

标签: jquery wordpress gravity-forms-plugin gravityforms


【解决方案1】:

假设您希望在单击第一个单选按钮时隐藏条件 div...

按名称将事件处理程序添加到单选按钮组。然后使用toggle和一个条件来判断这个div是否可见。

jQuery(function ($) {
   var trigger = $('[name=input_1]');
   trigger.change(function(){
       jQuery(".conditional-display").toggle(this.id !== "choice_13_1_0")       
   });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<ul class="gfield_radio" id="input_13_1">
<li class="gchoice_13_1_0">
    <input name="input_1" type="radio" value="Option One" id="choice_13_1_0" tabindex="1">
    <label for="choice_13_1_0" id="label_13_1_0">
        Option One
    </label>
</li>
<li class="gchoice_13_1_1">
    <input name="input_1" type="radio" value="Option Two" id="choice_13_1_1" tabindex="2">
    <label for="choice_13_1_1" id="label_13_1_1">
        Option Two
    </label>
</li>
<li class="gchoice_13_1_2">
    <input name="input_1" type="radio" value="Option Three" id="choice_13_1_2" tabindex="3">
    <label for="choice_13_1_2" id="label_13_1_2">
        Option Three
    </label>
</li>


<div class="conditional-display">
    Content to conditionally display based on radio field selection
</div>

【讨论】:

  • 是的!非常感谢你,你是救命稻草! ???今晚我会睡得安稳的
猜你喜欢
  • 2016-07-16
  • 2018-02-27
  • 2021-02-22
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多