【问题标题】:Toggle visibility of form fields if radio button is selected如果选择单选按钮,则切换表单字段的可见性
【发布时间】:2014-07-04 17:40:48
【问题描述】:

我最初试图隐藏一组表单域。然后,如果选择了单选按钮,则显示该组字段。然后,如果取消选择该收音机,请再次隐藏它们。

前两个工作正常(最初隐藏字段,如果选择了收音机,则显示它们)。但是当取消选择收音机时,我无法让它们再次隐藏。我确定我遗漏了一些明显的东西,但我无法弄清楚它是什么。

http://jsfiddle.net/C5PMy/

$(document).ready(function() {

toggleFields(); //call this first so we start out with the correct visibility depending on the selected form values

    //Hide the fields initially
    $("#bsd-field-custom-105966-group").hide();
    $("#bsd-field-custom-105967-group").hide();
    $("#bsd-field-custom-105968-group").hide();
    $("#bsd-field-custom-105978").hide();
    $("#bsd-field-custom-105969-group").hide();
    $("#bsd-field-custom-105970").hide();
    $("#bsd-field-custom-105979").hide();
    $("#bsd-field-custom-105971-group").hide();
    $("#bsd-field-custom-105972").hide(); 


//Show the fields only if lodging is required
       $("#custom-105965_0").change(function () {
    toggleFields();
});


});
//this toggles the visibility of the other lodging fields depending on the current     selected value of the "lodging required" field
function toggleFields() {
if ($(("#custom-105965_0").checked)) {

    $("#bsd-field-custom-105966-group").show();
    $("#bsd-field-custom-105967-group").show();
    $("#bsd-field-custom-105968-group").show();
    $("#bsd-field-custom-105978").show();
    $("#bsd-field-custom-105969-group").show();
    $("#bsd-field-custom-105970").show();
    $("#bsd-field-custom-105979").show();
    $("#bsd-field-custom-105971-group").show();
    $("#bsd-field-custom-105972").show();
            }

            else  {
    $("#bsd-field-custom-105966-group").hide();
    $("#bsd-field-custom-105967-group").hide();
    $("#bsd-field-custom-105968-group").hide();
    $("#bsd-field-custom-105978").hide();
    $("#bsd-field-custom-105969-group").hide();
    $("#bsd-field-custom-105970").hide();
    $("#bsd-field-custom-105979").hide();
    $("#bsd-field-custom-105971-group").hide();
    $("#bsd-field-custom-105972").hide();
            }
    }

【问题讨论】:

    标签: jquery radio-button toggle


    【解决方案1】:

    Fiddle Demo

    改变

    function toggleFields() {
        if ($("#custom-105965_0").is(':checked')) {
    

    和

    $(".custom-105965").change(function () {//assign change handler to both radio buttons
            toggleFields();
    });
    


    更好更短的代码版本

    Fiddle Demo

    $(document).ready(function () {
        $("[id^=bsd-field-custom-1059]").hide();
        $(".custom-105965").change(function () {
            if (this.value == 'Yes') {
                $("[id^=bsd-field-custom-1059]").show();
            } else {
                $("[id^=bsd-field-custom-1059]").hide();
            }
        });
    });
    


    更短的版本

    Fiddle Demo

    $(document).ready(function () {
        $("[id^=bsd-field-custom-1059]").hide();//hide all
        $(".custom-105965").change(function () {//change event on radio button
            $("[id^=bsd-field-custom-1059]").toggle(this.value == 'Yes'); //if selected radio button has value yes than return true i.e show else false i.e hide
        });
    });
    

    Attribute Starts With Selector [name^="value"]

    .toggle( showOrHide )

    【讨论】:

    • 非常感谢!我不能使用“属性开始于”部分来缩短代码,因为在活动页面上实际上有更多字段以我不想隐藏的相同字符串开头。但对以后的参考很有帮助。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 2015-03-25
    • 2019-12-13
    • 2011-10-28
    • 1970-01-01
    • 2012-12-09
    相关资源
    最近更新 更多