0

我想在通过 AJAX 加载的复选框上做这样的事情:

$('input:checkbox').each(function() {
        $(this).hide();
        alert("hi");
        $('<div class="checkbox-fx"><div class="checkbox"></div></div>').insertAfter(this);
    });

在文档加载时,复选框尚未加载,因此该功能未实现。如何循环浏览新加载的复选框?

谢谢!

4

2 回答 2

3

假设复选框是使用类似的东西加载的$.ajax,您的success方法将是在将生成的 HTML 添加到文档后处理生成的 HTML 的地方。

$.ajax({
 // your ajax options here
 success: function(html) {
    $(html).find('input:checkbox').each(function() {
        $(this).hide();
        alert("hi");
        $('<div class="checkbox-fx"><div class="checkbox"></div></div>').insertAfter(this);
    }).appendTo(document.body); // takes the HTML from the ajax call and processes each checkbox. when done, the results are then appended to the document
 }
});
于 2012-05-16T11:49:31.813 回答
0
    $.ajax({
    url: '/YourUrl', type: 'Get', dataType: 'json',
    // data: { id:'' },
     success: function(html) {
        $(html).find('input:checkbox').each(function() {
    if($(this).next('div.checkbox-fx').length===0)
    {
            $(this).hide();      
            $('<div class="checkbox-fx"><div class="checkbox"></div></div>').insertAfter(this);
}
        }).appendTo(document.body);

     }
    });
于 2012-05-16T11:59:20.200 回答