2011-04-01 71 views
0

我设置了两个数组:1)类名和数值名2)正在生效的HTML字段:如li,div等...jQuery:根据数组中的无线电盒显示/隐藏

基本上我设置所有使用类而不是ID的,因为#是一个coldfusion可见/功能标记,它引发了一个CFML错误。我把所有的东西都放到数组中以回复我必须写的$ .change(函数)的数量。

第一个问题:只有2个字段被隐藏。 第二个问题:当你点击一个收音机时,什么也没有显示出来。

有什么建议吗?

代码:

   <!--- An array of the radio boxes | 
        It's a collection of the class="" of the radio boxes 
        and their values. We're going to loop over this to 
        show/hide fields for the array below. 
       ---> 
       var classArray = new Array(
        "appliedWorkedYes", 
        "workStudyYes", 
        "workHistoryYes", 
        "workWeekEndsYes", 
        "cprYes", 
        "aedYes", 
        "aidYes" 
        ); 
       <!--- An array of the radio boxes extra fields, these will be hidden 
        It's a collection of HTML elements such as span or li that have the 
        class name of each in the array. the idea is on load they will be hidden 
        when you select yes on a radio box they will appear with their extra details. 
       ---> 
       var classFieldArray = new Array(
        "appliedWorkedYesHide", 
        "workStudyYesHide", 
        "workHistoryYesHide", 
        "workWeekEndsYesHide", 
        "cprYesHide", 
        "aedYesHide", 
        "aidYesHide"        
        )       

     $(document).ready(function() { 
       // looping over the class name 
       for(var i = 0; i < classArray.length; i++){ 
        var fName = classFieldArray[i]; 
        var cName = classArray[i]; 

         // hiding the fields until selected 
         $("."+fName).css("display","none"); 
         // the jQuery fu that shows the fields 
         $("radio[@name=''+cName]").change(function(){ 
          if ($("radio[@name=''+cName]:checked").val() == ''+cName) 
           $('.'+fName).show("fast"); 
          }); 
        } 
     }); 

回答

0

它看起来并不像你正确concatinating的CNAME。属性前面的@也不再使用。即:

$("radio[@name=''+cName]") 

应该

$("radio[name='"+cName+"']")