Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

Javascript/Jquery: Show a hidden div based on a Radio box select

What I'm doing is building a form, where when you select one answer a bunch of new questions pop up.

This is my working code:

$(".appliedWorked").click(function(){
   if($(this).val()==="appliedWorkedYes") 
      $(".appliedWorkedYesHide").show("fast"); 
   else 
      $(".appliedWorkedYesHide").hide("fast");
});

It works for only 1 class. I want to do this for many classes, so I thought I'd stick it into an array.

Here's my code for many classes but it's not showing when I hit yes on a radio box:

// storing an array of radio boxe class names
var radioBoxArray = new Array(
        "appliedWorkedYes",
        "workStudyYes",
        "workHistoryYes",
        "workWeekEndsYes",
        "cprYes",
        "aedYes",
        "aidYes",
        "wsiYes",
        "gaurdYes"
        );

// looping over the radio box array, too use the show feature of jquery
for(var j = 0; j < radioBoxArray.length; j++){
    // class name
    $("."+radioBoxArray[j]).click(function(){
        // value box
        if($(this).val()==='"+radioBoxArray[j]+"') 
            // show method
            $("."+radioBoxArray[j]+"Hide").show("fast"); 
        // hide else 
        else $("."+radioBoxArray[j]+"Hide").hide("fast");

    });

}

I think the issue is:

if($(this).val()==='"+radioBoxArray[j]+"') 

Please help!

I've tried the following but will not show when I click on a box:

if($(this).val()=== radioBoxArray[j])

if($(this).val()=== String( radioBoxArray[j] ))

if($(this).val()==='"'+radioBoxArray[j]+'"')
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Look at the syntax highlighting in your question. In

if($(this).val()==='"+radioBoxArray[j]+"')

the right-hand side of that test is just the string '"+radioBoxArray[j]+"'. Remove all those quotes.

if($(this).val() === radioBoxArray[j])

Other cleanup:

  • Declare the array using array literal notation:

    var radioBoxArray = [
    "appliedWorkedYes",
    "workStudyYes",
    "workHistoryYes",
    "workWeekEndsYes",
    "cprYes",
    "aedYes",
    "aidYes",
    "wsiYes",
    "gaurdYes"];
    
  • Save the radioBoxArray.length value in a local variable, otherwise it will be recomputed at every iteration. Also save the radioBoxArray[j] in a local variable (this also more efficient).

    var len = radioBoxArray.length,
        radio;
    for(var j = 0; j < len; j++){
        radio = radioBoxArray[j];
        // class name
        $("."+radio).click(function(){
            if($(this).val() === radio) $("."+radio+"Hide").show("fast");
            else $("."+radio+"Hide").hide("fast");
        });
    }
    
  • Instead of using separate show() and hide() calls, use .toggle(showOrHide)

Final code can be cleaned up like so:

var radioBoxArray = [
    "appliedWorkedYes",
    "workStudyYes",
    "workHistoryYes",
    "workWeekEndsYes",
    "cprYes",
    "aedYes",
    "aidYes",
    "wsiYes",
    "gaurdYes"
    ],

    len = radioBoxArray.length,
    radio;

for (var j = 0; j < len; j++) {
    radio = radioBoxArray[j];
    // class name
    $("." + radio).click(function() {
        $("." + radio + "Hide").toggle($(this).val() === radio);
    });
}

Alternatively, you could use $.each():

var radioBoxArray = [
    "appliedWorkedYes",
    "workStudyYes",
    "workHistoryYes",
    "workWeekEndsYes",
    "cprYes",
    "aedYes",
    "aidYes",
    "wsiYes",
    "gaurdYes"
    ];

$.each(radioBoxArray, function(i, v) {
    $("." + v).click(function() {
        $("." + v+ "Hide").toggle($(this).val() === v);
    });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...