开发者

jQuery nested array

Afternoon,

I have set up the following, the nested ifs are working however when i'd like to be able to pass t开发者_开发百科he title of the array to the alterFields() function, that way the function will cycle through each field stored in the array and apply the CSS class relative to that array (i.e. I have applied the same name to the array as the css class). Unfortunately if I use strings in the css array the $.each in alterfields cycles through each character of the string.

var assigned =[
"UAT Nominee"
];

var applications = [
"Primary Application Affected",
"Other Applications"
];

var comments = [
"Comments"
]

var css = [assigned, applications, comments];

    $.each(css, function(x){
    var current_class = css[x];
    alterfields(current_class);
    });

    function alterfields(array){
        $.each(array, function(i){
        var current_field = array[i];
    alert(current_field);
            $("#WebPartWPQ2 .ms-formlabel nobr").filter(function() {
                return $.text([this]) === array[i];
                }).closest('tr').toggleClass(array);
            });
        }

});

Thanks in Advance


Try something like this instead.

var fields = {
  assigned: [ "UAT Nominee" ],
  applications: [ "Primary Application Affected", "Other Applications" ],
  comments: [ "Comments" ]
};

var field_keys = [ 'assigned', 'applications', 'comments'];

$.each(field_keys, function(){
  alterfields( this );
});

function alterfields( field_key ){
  $.each(fields[ field_key ], function(){
    var current_field = this;
    $("#WebPartWPQ2 .ms-formlabel nobr").filter(function() {
      return $(this).text() === current_field;
    }).closest('tr').toggleClass( field_key );
  });
}


What you're doing isn't quite right. this line:

var css = [assigned, applications, comments];

makes CSS an numeric array of those numeric arrays. To reference those objects, you'll have to use css[0], css[1], etc. I do believe you want to exploit the fact that javascript objects can be treated as named arrays. so you would do something like this:

var css = {
  "assigned": assigned,
  "applications": applications,
  "comments": comments
};

Then when you reference css["assigned"] you get your numeric array that you're expecting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜