首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果只出现错误,提交后需要保留星级评分值

如果只出现错误,提交后需要保留星级评分值
EN

Stack Overflow用户
提问于 2017-08-20 06:25:15
回答 1查看 44关注 0票数 0

我有一个评级脚本,可以很好地工作,但当我提交评级时,在提交过程中出现了某种错误,星级评级返回到根本不显示任何星级。例如,假设我给出了两颗星的评级,然后提交了评级,在提交过程中发生了某种错误,由于某种原因,这些星又回到了零。我希望能够保持星级和价值,即使当一个错误发生时,这样当表单显示的错误,用户评级仍然是可见的,他们将不必重新输入他们的评级。然后,如果提交过程成功,星级评分和值可以恢复为默认值。

这是我的JsFiddle链接https://jsfiddle.net/znoxgg6k/

JQuery

代码语言:javascript
复制
$(document).ready(function(){
    var rating = 'eunrated';
    $('.rating-choice li')
        .on('mouseenter touchstart', function(){
            $(this).parent().removeClass($(this).parent().attr('class').split(' ')[1]);
            var classSuffix = $(this).attr('id').split('-')[1];
            $('.rating-choice').addClass('erating-' + classSuffix);
            $('.rate-this').text($(this).data('desc'));
        })
        .on('mouseleave touchend', function(){
            var classSuffix = $(this).attr('id').split('-')[1];
            $('.rate-this').text($('.rate-this').attr('data-desc'));
            $('.rating-choice').attr('class', '').addClass('rating-choice ' + rating)
        })
        .on('change click', function(e){
            e.preventDefault();
            e.stopPropagation();
            $('.rate-this').attr('data-desc', $(this).attr('data-desc'));
            rating = 'erating-' + $(this).attr('id').split('-')[1];
            $('.rating-choice').removeClass('eunrated').addClass(rating);
            $(this).find('input').prop('checked', true);
        });

        $('#clear-rating').on('change click', function(e){
            e.preventDefault();
            e.stopPropagation();
            $(this).parent().find('input:checked').prop('checked', false);
            $(this).parent().find('ul').removeClass().addClass('rating-choice eunrated');
            $('.rate-this').text($('.rate-this').attr('data-desc'));
            $('.rate-this').attr('data-desc', 'Rating').text('Rating');
            rating = 'eunrated';
        });
});

HTML

代码语言:javascript
复制
<form method="post" action="" id="form">
    <fieldset>
        <ol>
            <li><h2 class="header">Rating:</h2></li>
            <li>
                <ul class="rating-choice eunrated">
                    <li id="rate-1" data-desc="A">
                        <label for="rating-1"><input type="radio" value="1" name="rating" id="rating-1"  />0.5 stars</label>
                    </li>
                    <li id="rate-2" data-desc="B">
                        <label for="rating-2"><input type="radio" value="2" name="rating" id="rating-2"  />1 star</label>
                    </li>
                    <li id="rate-3" data-desc="C">
                        <label for="rating-3"><input type="radio" value="3" name="rating" id="rating-3"  />1.5 stars</label>
                    </li>
                    <li id="rate-4" data-desc="D">
                        <label for="rating-4"><input type="radio" value="4" name="rating" id="rating-4"  />2 stars</label>
                    </li>
                    <li id="rate-5" data-desc="E">
                        <label for="rating-5"><input type="radio" value="5" name="rating" id="rating-5"  />2.5 stars</label>
                    </li>
                    <li id="rate-6" data-desc="F">
                        <label for="rating-6"><input type="radio" value="6" name="rating" id="rating-6"  />3 stars</label>
                    </li>
                    <li id="rate-7" data-desc="G">
                        <label for="rating-7"><input type="radio" value="7" name="rating" id="rating-7"  />3.5 stars</label>
                    </li>
                    <li id="rate-8" data-desc="H">
                        <label for="rating-8"><input type="radio" value="8" name="rating" id="rating-8"  />4 stars</label>
                    </li>
                    <li id="rate-9" data-desc="I">
                        <label for="rating-9"><input type="radio" value="9" name="rating" id="rating-9"  />4.5 stars</label>
                    </li>
                    <li id="rate-10" data-desc="J">
                        <label for="rating-10"><input  type="radio" value="10" name="rating" id="rating-10"  />5 stars</label>
                    </li>
                </ul>
                <a id="clear-rating" title="" href="#">Clear</a>
                <div class="rate-this" data-desc="Rating">Rating</div>
            </li>
        </ol>
    </fieldset>
    <fieldset>  
        <ol>
            <li><input type="submit" name="submit" value="Submit" id="submit" /></li>
        </ol>
    </fieldset>
</form>

CSS

代码语言:javascript
复制
* {
    margin: 0;
    padding: 0;
    border: 0;
}

#form{
    padding: 2em 3% 0 3%;
}

#form ol li{
  list-style: none;
  width: 100%;
  float: left;
}

.rating-choice{
    width: 150px;
    height: 30px;
    float: left;
    margin-bottom: 1.8em;
}

.rating-choice input[type="radio"], .rating-choice label{
    height: 0 !important;
    display: none !important;
}

.rating-choice li{
    float: left !important;
    width: 15px !important;
    height: 30px !important; 
    display: block !important;
    list-style-type: none !important;
    cursor: pointer !important;
}

.header{
    font-size: 1em;
    display: inline-block;
    font-weight: bold;
    margin-bottom: 0.2em;
    width: 100%;
}

.eunrated{
    background-image: url('https://s18.postimg.org/mvu1nzei1/stars150.png');
    background-repeat: repeat-x;
    background-position: 0px -0px;
    width: 150px;
    height: 30px;
    margin: 0;
    display: inline-block;
}

.erating-1{
    background-image: url('https://s18.postimg.org/mvu1nzei1/stars150.png');
    background-repeat: repeat-x;
    background-position: 0px -30px;
    width: 150px;
    height: 30px;
    margin: 0;
    display: inline-block;
}

.erating-2{
    background-image: url('https://s18.postimg.org/mvu1nzei1/stars150.png');
    background-repeat: repeat-x;
    background-position: 0px -60px;
    width: 150px;
    height: 30px;
    margin: 0;
    display: inline-block;
}

.erating-3{
    background-image: url('https://s18.postimg.org/mvu1nzei1/stars150.png');
    background-repeat: repeat-x;
    background-position: 0px -90px;
    width: 150px;
    height: 30px;
    margin: 0;
    display: inline-block;
}

.erating-4{
    background-image: url('https://s18.postimg.org/mvu1nzei1/stars150.png');
    background-repeat: repeat-x;
    background-position: 0px -120px;
    width: 150px;
    height: 30px;
    margin: 0;
    display: inline-block;
}

.erating-5{
    background-image: url('https://s18.postimg.org/mvu1nzei1/stars150.png');
    background-repeat: repeat-x;
    background-position: 0px -150px;
    width: 150px;
    height: 30px;
    margin: 0;
    display: inline-block;
}

.erating-6{
    background-image: url('https://s18.postimg.org/mvu1nzei1/stars150.png');
    background-repeat: repeat-x;
    background-position: 0px -180px;
    width: 150px;
    height: 30px;
    margin: 0;
    display: inline-block;
}

.erating-7{
    background-image: url('https://s18.postimg.org/mvu1nzei1/stars150.png');
    background-repeat: repeat-x;
    background-position: 0px -210px;
    width: 150px;
    height: 30px;
    margin: 0;
    display: inline-block;
}

.erating-8{
    background-image: url('https://s18.postimg.org/mvu1nzei1/stars150.png');
    background-repeat: repeat-x;
    background-position: 0px -240px;
    width: 150px;
    height: 30px;
    margin: 0;
    display: inline-block;
}

.erating-9{
    background-image: url('https://s18.postimg.org/mvu1nzei1/stars150.png');
    background-repeat: repeat-x;
    background-position: 0px -270px;
    width: 150px;
    height: 30px;
    margin: 0;
    display: inline-block;
}

.erating-10{
    background-image: url('https://s18.postimg.org/mvu1nzei1/stars150.png');
    background-repeat: repeat-x;
    background-position: 0px -300px;
    width: 150px;
    height: 30px;
    margin: 0;
    display: inline-block;
}

#clear-rating{
    margin-left: 1em;
}

.rate-this{
    display: inline-block;
    width: 100%;
    margin-top: .5em;
    margin-bottom: 1em;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-20 06:44:07

通常,人们可能会将评论存储在数据库中,作为cookie或在会话中。这些解决方案需要使用诸如PHP、Node.js等的后端。

如果您想要一个仅限前端的解决方案,那么您可能需要看看sessionStoragelocalStorage应用编程接口。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45776810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档