开发者

Replacing/reloading input list with jQuery and leaving first element

I created jQuery function that gets bunch of simple objects that contains i开发者_Python百科d and title.

Then I run simple loop to create list:

$.each(data['results'], function(key, val) {
    items.push('<label for="radio_show_' + val['id'] + '" >' 
        + '<input type="checkbox" name="radio_show" class="checkbox" id="radio_show_' + val['id'] + '" value="'+ val['id'] + '" >'
        + val['title'] 
        + '</label>');
});

But now I don't know how to do the cleaning part.

I want to append this list to certain <ul>, I could do that with .appendTo(), but each time i would call this, it would add more and more lists. So before appending I would need to delete <ul> contains with .empty(). But the main problem is that I want to keep first element that would be same input just with no value and title of "Select all" (You get the idea).


Could you use something like this to 'clear' the ul -

$("ul > li:not(:first)").remove() 

That should remove all the items in the ul aprt from the first one.

Working demo - http://jsfiddle.net/zUuE9/


You could clear your ul with:

$('#my_ul input[value!=""]').parent('li').remove();

This would leave your Select All Checkbox (value="") in the ul.


To remove all li's except for the first one, try this:

$('#my-ul li').slice(1).remove();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜