简体   繁体   English

Javascript / Jquery:根据单选/选项框选择显示隐藏的div

[英]Javascript/Jquery: Show a hidden div based on a Radio/Option box select

Given this last thread: 给定最后一个线程:

Javascript/Jquery: Show a hidden div based on a Radio box select Javascript / Jquery:根据单选框选择显示隐藏的div

How do I adapt to make it work for both a radio box and a check box? 如何适应使其在单选框和复选框中都能使用?

Heres my code: 这是我的代码:

    $(document).ready(function() {
            /* To add more boxes to hide/show, add the name of the field to the array, 
                set the box class to that name + Hide */ 
            // storing the class names of the HTML elements we want to mess with
            var hiddenClassArray = [
                        "appliedWorkedYes",
                        "workStudyYes",
                        "workHistoryYes",
                        "workWeekEndsYes",
                        "cprYes",
                        "aedYes",
                        "aidYes",
                        "lifegaurd",
                        "wsiYes",
                        "gaurdYes",
                        "lifegaurdChk",
                        "fitnessChk",
                        "fitPTCYes",
                        "fitGrpYes",
                        "outdoorAdvChk",
                        "challengeChk",
                        "injuryCareChk",
                        "athTrainYes",
                        "athTrainYesHide"
                        ];  

            // looping over each array element, hiding them using jQuery
            for(var i = 0; i < hiddenClassArray.length; i++){
                // jQuery to append a display none. 
                $("."+hiddenClassArray[i]+"Hide").css("display","none");    
            }

            // ************ RADIO & CHECK BOXES ************

            // Show using toggle 
            $.each(hiddenClassArray, function(index, radio) {

            // first is it a Check box? 
            if($("."+radio).is(':checkbox')) {
                // toggle it then
                $("." + radio).click(function() {
                    $("." + radio + "Hide").toggle('fast');
                });
            } 
            // if it's a radio box
            else if ($("."+radio).is(':radio')) {
                // user clicked something
                $("." + radio).change(function() {   
                    // if it's yes, show
                    if($("."+radio).val()==="Yes") {
                        $("."+radio).show("slow");  
                    }
                    // hide it.
                    else{
                        $("."+radio).hide();
                    }
                }
            }
            }); // end .each
    }); // Ending the $(Doc) Ready                  

At first glance it looks like you simply need to check if the checkbox is checked. 乍一看,您似乎只需要检查该复选框是否已选中。 And than use that to show or hide your item. 而不是使用它来显示或隐藏您的项目。

$("." + radio).click(function() {
  $("." + radio + "Hide").toggle('fast', $(this).is(":checked"));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM