I am using a jQuery star rating plugin from here which works great, but on page load there is a function that gets called 11 times! The function should only get called when a rating is submitted ie a star is clicked. Is there anything I can do to stop it getting called on page load?
this is the function, which is in $(document).ready in my js file:
$(".auto-submit-star").rating({
callback: function (value, link) {
$.getJSON('/Handlers/RatingHandler.ashx?productId=' + pid + '&productScore=' + value, function (data) { postRatingUpdate(data); });
}
});
I don't know enough javascript to go poking round in the plugin's js file with any confidence, so am unsure what's happening behind the scenes.
this is the html that renders the rating stars:
<input class="auto-submit-star" type="radio" name="product-rating" value="1" />
<input class="auto-submit-s开发者_开发知识库tar" type="radio" name="product-rating" value="2"/>
<input class="auto-submit-star" type="radio" name="product-rating" value="3"/>
<input class="auto-submit-star" type="radio" name="product-rating" value="4"/>
<input class="auto-submit-star" type="radio" name="product-rating" value="5"/>
UPDATE: After trying some of the fixes mentioned, I realised that the rating method wasn't getting called multiple times, but the callback within it was, and as this contained my getJSON call, that was getting called multiple times. I think the multiple callbacks are needed convert the radiobuttons into stars.
The solution was to move the getJSON call into a separate funcion, and in the plugin's js file, add an OnClick to the html behind the star.
If rating is getting called onload. then it appears that rating is a method that should only be called when a radio is clicked.
$(".auto-submit-star").change(function(){
$(this).rating({
callback: function (value, link) {
$.getJSON('/Handlers/RatingHandler.ashx?productId=' + pid + '&productScore=' + value, function (data) { postRatingUpdate(data); });
}
});});
var stopMe = false;
if(!stopMe) {
stopMe = true
$(".auto-submit-star").rating({
callback: function (value, link) {
$.getJSON('/Handlers/RatingHandler.ashx?productId=' + pid + '&productScore=' + value, function (data) { postRatingUpdate(data);});
}
});
}
精彩评论