开发者

JQuery, array problem

开发者 https://www.devze.com 2023-02-13 00:58 出处:网络
It seems that the following code doesn\'t print anything, while, as you can see, it should. Basically there are no errors reported by Firebug.

It seems that the following code doesn't print anything, while, as you can see, it should. Basically there are no errors reported by Firebug.

            var assign = {
                'href' : {
                    '.voteUp' : '?p=action&a=voteup&p开发者_JS百科id='+ value.PostPID,
                    '.voteDown' : '?p=action&a=votedown&pid='+ value.PostPID,
                    '.postImage a': '?p=user&uid='+ value.UserUID
                },

                'src' : {
                    '.postImage img' : value.UserImage
                },

                'html' : {
                    '.repCount' : value.PostRep,
                    '.postInfo .rep': value.UserRep,
                    '.postInfo .name': value.UserName,
                    '.postInfo .time': value.PostTime,
                    '.postMessage' : value.PostText
                }
            };

            $.each(assign, function(type, data) {
                switch (type)
                {
                    case 'html':
                        $.each(data, function(handler, value) {
                            $('#'+ value.PostPID +' '+ handler).html(value);
                        });
                    break;

                    case 'href':
                    case 'src':
                        $.each(data, function(handler, value) {
                            $('#'+ value.PostPID +' '+ handler).attr(type, value);
                        });
                    break;
                }
            });

This is part of other code, but the rest of the script works well (for example, after this code there is a function that fadeIn the contents). If you cannot find anything bad here, please comment above and i'll add the entire script. Thanks.


None of the objects have a PostPID property.

Since value represents the object referenced b html, src etc., you need to use a property in those objects, to get the proper value.

For example:

case 'html':
    $.each(data, function(handler, value) {
        $('#'+ value['.repCount']+' '+ handler).html(value);
    });
break;

Or perhaps you wanted the other value identifier (the source of which is not included in the question).

In that case, rename the parameter for the $.each() handler to something else.

case 'html':
       // renamed value paramter---v---to make the previous "value" accessible
    $.each(data, function(handler, v) {
        $('#'+ value.PostPID+' '+ handler).html(v['.repCount']);
  // original----^        "each" parameter------^
    });
break;
0

精彩评论

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