【问题标题】:jQuery Show/Hide <div> for multiple unrelated sets of radio buttons within a single pagejQuery Show/Hide <div> 用于单个页面中的多个不相关的单选按钮集
【发布时间】:2020-07-01 07:56:54
【问题描述】:

我正在处理一个带有不同单选按钮集(是/否,两个选项)的长 html 表单,每个单选按钮对应于根据选择出现的单独隐藏字段。

我最初的解决方案是使用 jQuery 的 Show/Hide 函数来切换“hiddenfield”类的显示:无;通过将单选按钮的值与第二个类匹配来设置和定位包含隐藏字段的 div。但是,无论如何,我都不是 jQuery 专家,我的简单尝试无法区分不同的单选按钮集。相反,一次只能显示一个隐藏字段。所以我正在寻找一个更完整的 jQuery 或 javascript 解决方案,它允许我取消隐藏特定类(在我的情况下为隐藏字段),但前提是 div 还包含一个等于所选单选按钮值的类。所有其他隐藏字段应保持各自的隐藏/显示状态。

提前感谢您的帮助。

这是我当前的代码 sn-ps:

$(document).ready(function(){
    $('input[type="radio"]').click(function(){
        var inputValue = $(this).attr("value");
        var targetHiddenfield = $("." + inputValue);
        $(".hiddenfield").not(targetHiddenfield).hide();
        $(targetHiddenfield).show();
    });
});
.hiddenfield {display: none;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="cta__sub cta__sub--center">Do the members want to have meetings and formal voting processes or would they prefer to act with little formality.</p>
                <div class="double">
                  <p class="half">
                    <input name="formal_yesno" type="radio" id="radio9" value="formal_yes" />
                    <label for="radio9">Yes, formal meetings and voting.</label>
                  </p>
                  <p class="half">
                    <input name="formal_yesno" type="radio" id="radio10" value="formal_no" />
                    <label for="radio10">No, we will operate informally.</label>
                  </p>
                </div>
                
                <div class="formal_yes hiddenfield">
                <p class="cta__sub cta__sub--center">Will all members be entitled to notice and to have their voice heard, or will the minimum number for a majority decision be enough?</p>
                <div class="double">
                  <p class="half">
                    <input name="formal_y_extra" type="radio" id="radio11" value="formal_y_extra_yes" />
                    <label for="radio11">Yes, all members will be notified and heard.</label>
                  </p>
                  <p class="half">
                    <input name="formal_y_extra" type="radio" id="radio12" value="formal_y_extra_yes" />
                    <label for="radio12">No, only a majority is required.</label>
                  </p>
                </div>
                </div>
                <div class="formal_no hiddenfield">
                <p class="cta__sub cta__sub--center">Will some decisions require formal meetings and voting procedures?</p>
                <div class="double">
                  <p class="half">
                    <input name="formal_n_extra" type="radio" id="radio13" value="formal_n_extra_yes" />
                    <label for="radio13">Yes</label>
                  </p>
                  <p class="half">
                    <input name="formal_n_extra" type="radio" id="radio14" value="formal_n_extra_no" />
                    <label for="radio14">No</label>
                  </p>
                </div>
                </div>
                 <div class="formal_n_extra_yes hiddenfield">
                     <p class="cta__sub cta__sub--center">Please explain</p>
                    <textarea name='message' placeholder="Formal meetings and voting will occur...." id='textarea'></textarea>
                    </div>

【问题讨论】:

    标签: jquery html


    【解决方案1】:

    在 jQuery 中的 &lt;script&gt; 标记中使用它。

    (.Role13).Click(function({
         (".Role9").show();
    })
    

    【讨论】:

    • 由于我对 jQuery 了解不多,我不确定如何根据我的情况调整您的建议。对我来说,看起来我必须为每组单选按钮复制该功能,以防止它们与其他按钮交互(如果我错了,请告诉我)。由于有一百多个集合,并且该项目的长期目标需要动态更改集合的数量,我试图用尽可能少的行找出一个通用的解决方案,以防止我的文件大小在下一阶段的发展。
    • 请简要说明您想做什么。
    • 我正在尝试构建一个 html 表单。选择某些单选按钮后,应出现文本框以回答其他问题。大约有 100 个带有文本框的单选按钮。我上面的解决方案一次只适用于一组单选按钮。当您选择下一个单选按钮时,先前的文本框会消失。数据仍然在提交时保存,但用户很难返回并编辑他们的答案。我正在寻找一种解决方案,该解决方案允许打开或关闭所有或任何一个单选框,而不管任何其他单选按钮状态如何。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多