我已经实现了一个 jquery 插件评级小部件。在我进行 ajax 调用以获取数据之前,它工作得很好。我需要插件来添加新元素。
我试图将脚本封装在一个函数中,并在成功时再次调用该函数,但没有运气。
如果没有成功,我也无法调用该函数,所以这是我的主要问题。
我如何将它变成一个函数并在页面加载和 ajax 调用时调用它。正如您在下面看到的,我尝试将其包围。
function RatingWidget(e) {
// jQuery.noConflict();
$(document).ready(function() {
//we bind only to the rateit controls within the products div
//$('#products .rateit').bind('rated reset', function (e) {
$(".rateit").bind('over', function(event, value) {
$(this).attr('title', value);
});
$(" .rateit").bind('rated reset', function(e) {
var ri = $(this);
// var msg = ri.next();
// var msg = ri.next('div .ratemsg');
var msg = $(this).parents().nextAll("div.ratemsg:first")
//if the use pressed reset, it will get value: 0 (to be compatible with the HTML range control), we could check if e.type == 'reset', and then set the value to null .
var value = ri.rateit('value');
//var productID = ri.data('productid'); // if the product id was in some hidden field: ri.closest('li').find('input[name="productid"]').val()
var ratingid = ri.data('ratingid');
// var ratingtest = ri.data('ratingtest');
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var dateoftweet = (month + "/" + day + "/" + year)
// var dateoftweetparse = Date.parse(dateoftweet);
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10) {
minutes = "0" + minutes
}
var timeoftweet = (hours + ":" + minutes + " ")
// var timeoftweetparse = Date.parse(timeoftweet);
//maybe we want to disable voting?
ri.rateit('readonly', true);
$.ajax({
url: '/Home/GetRating',
//your server side script
data: {
id: ratingid,
value: value,
tweetday: dateoftweet,
tweettime: timeoftweet
},
//our data
type: 'POST',
success: function(data) {
//$('#ratemsg').html(data);
msg.html("<B>The TweetId is " + ratingid + " the vote value is " + value + " " + dateoftweet + " " + timeoftweet + "</b>");
},
error: function(jxhr, msg, err) {
// $('#response').append('<li style="color:red">' + msg + '</li>');
msg.html(data);
}
});
});
});
}