0

So I have the following code :

function aFunction(obj, myData)
{
    $.ajax({
        type: "POST",
        url: 'myUrl',
        data: myData,
        async: true,
        dataType: 'json',
        success: function(data)
        {
            // ???
        }
    });
}

The data I receive (correctly) is an array of booleans. Now I need to show the jQuery objects according to each boolean : the number of jQuery objects and the size of the array is the same.

Now, the following works as my success callback :

    success: function(data)
    {
        //obj.show();
        var pcom_array = $.makeArray(obj);

        for(var i = 0; i < data.length; i++)
        {
            console.log(pcom_array[i]);
            if(data[i] === true)
            {
                pcom_array[i].style.display = '';
            }
        }
    }

However, casting the jQuery object as an array and then iterating over the array doesn't feel very "jQuery-ish" to me. Is there a jQuery method or some other way that could smplify this?

4

1 回答 1

2

You can use .toggle() - assuming obj is a jQuery wrapper containing a set of targeted elements in the same order as in the data array

success: function(data) {
    obj.each(function(idx, el){
        $(this).toggle(data[idx])
    }) 
}
于 2013-08-30T16:05:50.833 回答