问题描述
我正在使用jRaty插件显示星级"评分-显示星级的代码相对简单(当我在页面中显示html代码时可以使用)-问题是我正在使用ajax调用使用JSON重新填充页面-即使跨度以与下面的html代码相同的方式填充,也不会使用插件显示星级图片.
I am using the jRaty plugin to display a 'star' rating - the code to display the star rating is relatively simple (and works when I display the html code in my page) - the problem is I am using an ajax call to re-populate the page using JSON - even though the span is populated in the same manner as the html code below it doesn't display the star rating image using the plugin.
我相信这可能与事件委托有关(尽管我不是jQuery专家)有人可以解释为什么我的jRaty插件在通过ajax/json方法时不起作用-任何人都有想法吗?
I believe this may have something to do with event delegation (although i'm no jQuery expert) Can anyone explain why my jRaty plugin doesn't work when via the ajax/json method - anyone got any ideas?
// html code
<span class="stars" data-score="4"></span>
/* star rating */
$( ".stars" ).raty( {
half: true,
hints: [ "1 Star", "2 Star", "3 Star", "4 Star", "5 Star" ],
path: "/assets/js/jquery-plugins/jquery-raty/2.5.2/img",
readOnly: true,
score: function( ){
return $(this).attr( "data-score" );
},
size: 24,
starHalf: "star-half-big.png",
starOff: "star-off-big.png",
starOn: "star-on-big.png"
} );
更新..
我已经在网页的两个位置加载了jRaty,一个是静态",另一个是动态的(仅在jQuery AJAX调用之后显示.
I have loaded the jRaty in two places on my webpage, one is 'static' and another is dynamic (only shown after the jQuery AJAX call.
当我检查jRaty星级的静态"迭代时,会在settings属性上显示jQuery/jRaty元素,而后者对jRaty星级的动态"迭代却没有.
The 'static' iteration of the jRaty star rating shows the jQuery/jRaty element on the settings attribute when I inspect the element, whereas the latter 'dynamic' iteration of the jRaty star rating doesn't.
看起来不只是我这个问题.. doh如何将raty插件应用于新生成的div?
Looks like it isn't just me with this problem.. dohHow to apply raty plugin to new generated divs?
推荐答案
以防万一这可能对其他人有所帮助.
Just in case this might help someone else..
我为AJAX成功添加了以下内容
I added the following to the AJAX success
success: function(data){
$( ".stars" ).raty( {
half: true,
hints: [ "1 Star", "2 Star", "3 Star", "4 Star", "5 Star" ],
path: "/assets/js/jquery-plugins/jquery-raty/2.5.2/img",
readOnly: true,
score: function( ){
return $(this).attr( "data-score" );
},
size: 24,
starHalf: "star-half-big.png",
starOff: "star-off-big.png",
starOn: "star-on-big.png"
} );
}
这篇关于jRaty jQuery插件似乎不适用于AJAX/JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!