2013-01-04 75 views
2

我使用一個jQuery rating plugin,我使用這個在這樣一個div:插件不使用Ajax加載內容的工作

HTML:

<div id="highlighted" class="span6"> 
<div class="row"> 
    <div id="high-desc" class="span4"> 
     <span class="movie-title fat"><a class="strong" href="movies.jsp?id=5">Watchmen</a> (2009)</span><br /> 
     <div id="rating"></div> 
    </div> 
</div> 

的Jquery:

$("#rating").rateit({ 
    max: 10, 
    min: 0, 
    step: 1 
}); 

它效果很好。但是,當我用其他內容(如下)替換div#highlighted的內容時,插件不起作用。

$('// some link').on("click", function(){ 
    var Id = // some id 
    title="reco_product_stream.jsp?type=high&id="+Id; 
    ('#highlighted').load(title); 
}); 

我甚至嘗試重裝就這樣每次Ajax調用插件:

$('// some link').on("click", function(){ 
    var Id = // some id 
    title="reco_product_stream.jsp?type=high&id="+Id; 
    ('#highlighted').load(title); 
    $("#rating").rateit({max: 10, min: 0, step:1}); 
}); 

但它不工作。

+0

這只是一個建議,但下面的官方例子可以幫助你:http://www.radioactivethinking.com/rateit/example/example.htm#ex_11 (例如#11 ) –

回答

2
$('// some link').on("click", function(){ 
    var Id = // some id 
    title="reco_product_stream.jsp?type=high&id="+Id; 
    $('#highlighted').load(title); 
    $("#rating").rateit({max: 10, min: 0, step:1}); 
}); 

以上代碼$("#rating").rateit({max: 10, min: 0, step:1});在.load完成加載之前執行。

與.load回調一起使用。

$('#highlighted').load(title, function(){ 
$("#rating").rateit({max: 10, min: 0, step:1}); 
}); 

http://api.jquery.com/load/

+0

謝謝!這是在jQuery加載內容上啓用jquery的標準方式,即通過將腳本作爲回調運行,還是僅僅是解決方法? – soundswaste

+0

默認情況下,所有請求都是在您需要回調方法時異步發送的。 –

3

你確實需要重新初始化動態地添加內容的插件。

爲此,您必須等待通過使用回調函數完成請求。

jQuery的load方法與大多數ajax方法一樣是異步的。

http://api.jquery.com/load/

$('// some link').on("click", function(){ 
    var Id = // some id 
    title="reco_product_stream.jsp?type=high&id="+Id; 
    ('#highlighted').load(title,function(){ //callback executes after load 
     $("#rating").rateit({max: 10, min: 0, step:1}); 
    }); 
});