2011-01-19 68 views
0

我有一些jQuery代码来设置我的页面上的一些复选框。我也有一张桌子。此表不显示加载页面时的所有行,因此您必须使用ajax请求加载剩余的行。问题在于,ajax加载的行中的复选框不会被设置样式。我想继承复选框样式到已经加载了ajax的复选框。加载jQuery代码时继承jQuery代码

回答

0

一旦新的复选框被加载,你将不得不触发你的jQuery代码来设置这些复选框的样式(只有新的复选框)。没有看到你的代码或标记是不可能给你一个直接的答案,但是,例如,假设你的jQuery代码添加了一个类到复选框(大概作为它的一部分,而不是全部),否则你只需要使用CSS造型)。然后,你只需有一个代码,一定要留出这些复选框:

var cbsToStyle = $(":checkbox:not(.foo)"); 

...然后在样式列表cbsToStyle

(live copy)

HTML(明显不一样的你,但表示):

<div id='container'> 
    <label><input type='checkbox'>Original checkbox</label> 
    <br><label><input type='checkbox'>Original checkbox</label> 
    <br><label><input type='checkbox'>Original checkbox</label> 
</div> 

JavaScript的使用jQuery:

jQuery(function($) { 
    var timer = 0, 
     counter = 4; 

    styleCheckboxes(); 
    timer = setInterval(loadCheckboxes, 1000); 

    function loadCheckboxes() { 
    var index; 

    if (--counter < 0) { 
     // Done 
     cancelInterval(timer); 
     timer = 0; 
    } 
    else { 
     // Mimic adding three checkboxes 
     for (index = 0; index < 3; ++index) { 
     $("<br><label><input type='checkbox'>Another checkbox</label>").appendTo('#container'); 
     } 

     // Re-trigger the styling code 
     styleCheckboxes(); 
    } 
    } 

    function styleCheckboxes() { 
    // Get only those we haven't styled before and style them. 
    // Note that the class is used as a marker (you might _also_ 
    // use it to apply some style rules) 
    $(":checkbox:not(.styled)").addClass("styled").css("width", "40px"); 
    } 

});​ 

...其中styleCheckboxes是最相关位。

+0

那很简单。谢谢! – Emil 2011-01-19 14:09:12