jQuery:基于数组中的Radio Box显示/隐藏

时间:2011-04-01 21:29:39

标签: jquery

我设置了两个数组:1)类名和值名称2)正在生效的HTML字段:例如li,divs等......

基本上我将所有内容设置为使用类而不是ID,因为#是一个coldfusion veriable / function标记,它会引发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");
                            });
                    }
        });

1 个答案:

答案 0 :(得分:0)

看起来你没有正确地合并cName。此外,不再使用@在属性前面。即:

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

应该是

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