How to use Star rating of Font-awesome with Django?

后端 未结 11 1920
挽巷
挽巷 2020-12-23 17:37

Fontawesome has a great star rating css extension, which looks really awesome.

However clicking on any of the span elements wouldn\'t really do anything. I don\'t

11条回答
  •  鱼传尺愫
    2020-12-23 17:55

    I did not see a simple answer using only bootstrap glyphicons and jquery. I am sure other people have come here looking for a quick copy and paste so I just wrote one.

    $(function(){
        $('.rating-select .btn').on('mouseover', function(){
            $(this).removeClass('btn-default').addClass('btn-warning');
            $(this).prevAll().removeClass('btn-default').addClass('btn-warning');
            $(this).nextAll().removeClass('btn-warning').addClass('btn-default');
        });
    
        $('.rating-select').on('mouseleave', function(){
            active = $(this).parent().find('.selected');
            if(active.length) {
                active.removeClass('btn-default').addClass('btn-warning');
                active.prevAll().removeClass('btn-default').addClass('btn-warning');
                active.nextAll().removeClass('btn-warning').addClass('btn-default');
            } else {
                $(this).find('.btn').removeClass('btn-warning').addClass('btn-default');
            }
        });
    
        $('.rating-select .btn').click(function(){
            if($(this).hasClass('selected')) {
                $('.rating-select .selected').removeClass('selected');
            } else {
                $('.rating-select .selected').removeClass('selected');
                $(this).addClass('selected');
            }
        });
    });
    
    
    

    To set the default value from the DB using django template, for each star do:

提交回复
热议问题