结合使用jQuery .css()和变量

I'm using jQuery to dynamically change the background image on a div. Here's my code:

$.ajax({
    url: '/countries/' + currentHash,
    type: 'GET',
    dataType: 'html',
    complete: function(xhr, textStatus) {
        //called when complete
    },
    success: function(data, textStatus, xhr) {
        $('#info').empty();

        var country_id = $('div#country_id', data).text();
        $('#fancybox-outer').css('background', "#ffffff url(\"/system/backgrounds/" + country_id + "/large.jpg\") no-repeat top right");
        // $('#fancybox-outer').css('background', '#ffffff url("/system/backgrounds/4/large.jpg") no-repeat top right');

        $.fancybox(
            $('#info').html(),
            {
                autoDimensions   : false,
                width            : '800',
                height           : '600',
                speedIn              : 600,
                speedOut             : 200,
                // scrolling             : 'no',
                padding : 0,
                centerOnScroll : true,
                overlayColor : '#333333',
                overlayOpacity : 0.8,
                transitionIn : 'fade', // 'elastic', 'fade' or 'none'
                transitionOut : 'elastic', // 'elastic', 'fade' or 'none'
                hideOnOverlayClick : false,
            }
        );                                                      

    }
});

Everything else is working great in the code. I actually took some out so that it's easier for your to read. It's getting the country_id within the div. I've tried appending the country_id variable into a div on the page to make sure that it's getting it and it is. But this part just isn't working:

    var country_id = $('div#country_id', data).text();
    $('#fancybox-outer').css('background', "#ffffff url(\"/system/backgrounds/" + country_id + "/large.jpg\") no-repeat top right");
    // $('#fancybox-outer').css('background', '#ffffff url("/system/backgrounds/4/large.jpg") no-repeat top right');

The commented out part works. Any thoughts on what I should do?

Thanks!

Try modifying the quotes like this:

$('#fancybox-outer').css('background', '#ffffff url("/system/backgrounds/' + country_id + '/large.jpg") no-repeat top right');

You may also need to make sure that country_id doesn't contain any whitespace

var country_id = $('div#country_id', data).text().trim();