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" />
...
$('.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.');
}
});
});
you could unbind the click event (using
off()) after the ajax callI suppose you may have several
.starelements so you should unbind the handler attached on the clicked link only.Another simpler possibility is to bind the handler using
.one(), like sothis will allow one click event only for each different
.starelementAnyway, 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