Title: Disable jquery star plugin after user rate
Tags: javascript;jquery-ui;jquery;jquery-plugins
Question: I have implemented jquery star rating. When user add its rate I save it to database and disable star rating.
But after disabling if user click on star again it again call js function and try to save score to database.
<input name="rating" type="radio" class="star" value="3" /&gt;
$('.star').click(function(event) {
$.ajax({
type: "POST",
url: '@Url.Action("Rate", "Rating")',
data: $("#rate").serialize(),
success: function(response) {
alert('Your rating has been recorded');
$('.star').rating('disable', true);
},
error: function(response) {
alert('There was an error.');
}
});
});

Here is the accepted answer: you could unbind the click event (using off()) after the ajax call
$('.star').click(function(event) {
var $this = $(this);
...
success: function(response) {
alert('Your rating has been recorded');
$this.off('click');
},
I suppose you may have several .star elements so you should unbind the handler attached on the clicked link only.
Another simpler possibility is to bind the handler using .one(), like so
$('.star').one('click', function(event) {
var $this = $(this);
...
success: function(response) {
alert('Your rating has been recorded');
},
this will allow one click event only for each different .star element
Anyway, I strongly suggest you to make this kind of check also on server side. Dont rely on client side for actions that may alter data on the server
Comment for this answer: I will :) Thanks I didn't know about '.one'


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:09/03/12', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<javascript><jquery-ui><jquery><jquery-plugins>', 'Title': 'Disable jquery star plugin after user rate', 'ViewCount': 1712, 'id': 12251301, 'user_AboutMe': '', 'user_AccountId': 221962.0, 'user_DisplayName': '1110', 'user_Reputation': 7725.0, 'user_Views': 1610.0, 'user_id': 480231.0}}