当焦点丢失时,Ajax 星级会重置 (jquery)

发布于 2024-07-24 07:58:36 字数 2721 浏览 5 评论 0原文

我正在使用以下 AJAX 评级系统代码

jQuery.fn.rater = function(url, options)
{
    if(url == null) return;
    var settings = {
        url       : url, // post changes to 
        maxvalue  : 5,   // max number of stars
        curvalue  : 0    // number of selected stars
    };

    if(options) { jQuery.extend(settings, options); };
    jQuery.extend(settings, {cancel: (settings.maxvalue > 1) ? true : false});

    var container = jQuery(this);
    jQuery.extend(container, { averageRating: settings.curvalue, url: settings.url });

    var raterwidth = settings.maxvalue * 25;
    var ratingparent = '<ul class="star-rating" style="width:'+raterwidth+'px">';

    container.append(ratingparent);

    // create rater
    var starWidth, starIndex, listitems = '';
    var curvalueWidth = Math.floor(100 / settings.maxvalue * settings.curvalue);
    for(var i = 0; i <= settings.maxvalue ; i++) {
        if (i == 0) {
            listitems+='<li class="current-rating" style="width:'+curvalueWidth+'%;">'+settings.curvalue+'/'+settings.maxvalue+'</li>';
        } else {
            starWidth = Math.floor(100 / settings.maxvalue * i);
            starIndex = (settings.maxvalue - i) + 2;
            listitems+='<li class="star"><a href="#'+i+'" title="'+i+'/'+settings.maxvalue +'" style="width:'+starWidth+'%;z-index:'+starIndex+'">'+i+'</a></li>';
        }
    }
    container.find('.star-rating').append(listitems); // i am using find here, because the span wrapped in the small style would break children()

    if(settings.maxvalue > 1) // add a container for the ajax result
    {
        container.append('<span class="star-rating-result"></span>'); 
    }
    var stars = jQuery(container).find('.star-rating').children('.star');
    stars.click(function()
    {
        if(settings.maxvalue == 1) // on / off
        {
            settings.curvalue = (settings.curvalue == 0) ? 1 : 0;
            jQuery(container).find('.star-rating').children('.current-rating').css({width:(settings.curvalue*100)+'%'});
            jQuery.post(container.url, { "rating": settings.curvalue });

            return false;
        }
        else
        {

            settings.curvalue = stars.index(this) + 1;
            raterValue = jQuery(this).children('a')[0].href.split('#')[1];
            jQuery.post(container.url, { "rating": raterValue }, function(response){
                container.children('.star-rating-result').html(response)    
            });
            return false;
        }
        return true;
    });

    return this; 
}

问题是,当我单击星星以外的其他内容时,评级会自行重置。

当焦点消失时,您能否建议一些方法来使评级保持其价值?

I am using following code for AJAX rating system

jQuery.fn.rater = function(url, options)
{
    if(url == null) return;
    var settings = {
        url       : url, // post changes to 
        maxvalue  : 5,   // max number of stars
        curvalue  : 0    // number of selected stars
    };

    if(options) { jQuery.extend(settings, options); };
    jQuery.extend(settings, {cancel: (settings.maxvalue > 1) ? true : false});

    var container = jQuery(this);
    jQuery.extend(container, { averageRating: settings.curvalue, url: settings.url });

    var raterwidth = settings.maxvalue * 25;
    var ratingparent = '<ul class="star-rating" style="width:'+raterwidth+'px">';

    container.append(ratingparent);

    // create rater
    var starWidth, starIndex, listitems = '';
    var curvalueWidth = Math.floor(100 / settings.maxvalue * settings.curvalue);
    for(var i = 0; i <= settings.maxvalue ; i++) {
        if (i == 0) {
            listitems+='<li class="current-rating" style="width:'+curvalueWidth+'%;">'+settings.curvalue+'/'+settings.maxvalue+'</li>';
        } else {
            starWidth = Math.floor(100 / settings.maxvalue * i);
            starIndex = (settings.maxvalue - i) + 2;
            listitems+='<li class="star"><a href="#'+i+'" title="'+i+'/'+settings.maxvalue +'" style="width:'+starWidth+'%;z-index:'+starIndex+'">'+i+'</a></li>';
        }
    }
    container.find('.star-rating').append(listitems); // i am using find here, because the span wrapped in the small style would break children()

    if(settings.maxvalue > 1) // add a container for the ajax result
    {
        container.append('<span class="star-rating-result"></span>'); 
    }
    var stars = jQuery(container).find('.star-rating').children('.star');
    stars.click(function()
    {
        if(settings.maxvalue == 1) // on / off
        {
            settings.curvalue = (settings.curvalue == 0) ? 1 : 0;
            jQuery(container).find('.star-rating').children('.current-rating').css({width:(settings.curvalue*100)+'%'});
            jQuery.post(container.url, { "rating": settings.curvalue });

            return false;
        }
        else
        {

            settings.curvalue = stars.index(this) + 1;
            raterValue = jQuery(this).children('a')[0].href.split('#')[1];
            jQuery.post(container.url, { "rating": raterValue }, function(response){
                container.children('.star-rating-result').html(response)    
            });
            return false;
        }
        return true;
    });

    return this; 
}

The problem is that when I click on something else other than stars, rating reset itself.

Could you suggest some ways to make the rating retain its value, when the focus is lost?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文