在同一页面中添加多个星级评分

时间:2017-04-29 07:43:53

标签: jquery html css asp.net-mvc-5

我试图在mvc中为我的页面添加星级评分功能,但是当我试图在视图中循环它们时我遇到的问题是所有星星都附加到第一个上面意味着当我改变第二个是第一个改变,我从互联网上获得了代码 任何人都可以提供帮助

fieldset, label { margin: 0; padding: 0; }
body{ margin: 20px; }
h1 { font-size: 1.5em; margin: 10px; }

/****** Style Star Rating Widget *****/

.rating { 
 border: none;
 float: left;
  }

.rating > input { display: none; } 
.rating > label:before { 
 margin: 5px;
 font-size: 1.25em;
 font-family: FontAwesome;
 display: inline-block;
 content: "\f005";
  }

 .rating > .half:before { 
  content: "\f089";
  position: absolute;
   }

   .rating > label { 
    color: #ddd; 
    float: right; 
     }

     /***** CSS Magic to Highlight Stars on Hover *****/

    .rating > input:checked ~ label, /* show gold star when clicked */
     .rating:not(:checked) > label:hover, /* hover current star */
      .rating:not(:checked) > label:hover ~ label { color: #FFD700;  } 
       /*        hover previous stars in list */

       .rating > input:checked + label:hover,
       .rating > input:checked ~ label:hover,
       .rating > label:hover ~ input:checked ~ label, 
       .rating > input:checked ~ label:hover ~ label { color: #FFED85;  }

HTML:

  <h1>Pure CSS Star Rating Widget</h1>
<fieldset class="rating">
    <input type="radio" id="star5" name="rating" value="5" /><label class = "full" for="star5" title="Awesome - 5 stars"></label>
    <input type="radio" id="star4half" name="rating" value="4 and a half" /><label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
    <input type="radio" id="star4" name="rating" value="4" /><label class = "full" for="star4" title="Pretty good - 4 stars"></label>
    <input type="radio" id="star3half" name="rating" value="3 and a half" /><label class="half" for="star3half" title="Meh - 3.5 stars"></label>
    <input type="radio" id="star3" name="rating" value="3" /><label class = "full" for="star3" title="Meh - 3 stars"></label>
    <input type="radio" id="star2half" name="rating" value="2 and a half" /><label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>
    <input type="radio" id="star2" name="rating" value="2" /><label class = "full" for="star2" title="Kinda bad - 2 stars"></label>
    <input type="radio" id="star1half" name="rating" value="1 and a half" /><label class="half" for="star1half" title="Meh - 1.5 stars"></label>
    <input type="radio" id="star1" name="rating" value="1" /><label class = "full" for="star1" title="Sucks big time - 1 star"></label>
    <input type="radio" id="starhalf" name="rating" value="half" /><label class="half" for="starhalf" title="Sucks big time - 0.5 stars"></label>
</fieldset>

1 个答案:

答案 0 :(得分:2)

当您尝试多次插入相同的代码时,有两个问题:

    HTML中的
  • 应该只有1个元素具有特定的 ID
  • 通过 NAME 属性将无线电元素组合在一起,这意味着如果您的所有输入都具有相同的名称(例如评级),则所有无线电输入都会更改该变量的值

这是解决这个问题的方法:

  1. 您应该为每个单独的字段集块的ID添加唯一标识符
  2. 您应在每个字段集块中使用单独的名称属性
  3. 请参阅此处的示例:https://jsfiddle.net/uf3opady/5/

    代码(请注意我在代码中使用 _1 _2 后缀作为 ID,FOR和NAME属性:< / p>

    <h1>Pure CSS Star Rating Widget</h1>
    <fieldset class="rating">
        <input type="radio" id="star5_1" name="rating_1" value="5" /><label class = "full" for="star5_1" title="Awesome - 5 stars"></label>
        <input type="radio" id="star4half_1" name="rating_1" value="4 and a half" /><label class="half" for="star4half_1" title="Pretty good - 4.5 stars"></label>
        <input type="radio" id="star4_1" name="rating_1" value="4" /><label class = "full" for="star4_1" title="Pretty good - 4 stars"></label>
        <input type="radio" id="star3half_1" name="rating_1" value="3 and a half" /><label class="half" for="star3half_1" title="Meh - 3.5 stars"></label>
        <input type="radio" id="star3_1" name="rating_1" value="3" /><label class = "full" for="star3_1" title="Meh - 3 stars"></label>
        <input type="radio" id="star2half_1" name="rating_1" value="2 and a half" /><label class="half" for="star2half_1" title="Kinda bad - 2.5 stars"></label>
        <input type="radio" id="star2_1" name="rating_1" value="2" /><label class = "full" for="star2_1" title="Kinda bad - 2 stars"></label>
        <input type="radio" id="star1half_1" name="rating_1" value="1 and a half" /><label class="half" for="star1half_1" title="Meh - 1.5 stars"></label>
        <input type="radio" id="star1_1" name="rating_1" value="1" /><label class = "full" for="star1_1" title="Sucks big time - 1 star"></label>
        <input type="radio" id="starhalf_1" name="rating_1" value="half" /><label class="half" for="starhalf_1" title="Sucks big time - 0.5 stars"></label>
    </fieldset>
    
    <br><br><br>
    
    <h1>2nd Pure CSS Star Rating Widget</h1>
    <fieldset class="rating">
        <input type="radio" id="star5_2" name="rating_2" value="5" /><label class = "full" for="star5_2" title="Awesome - 5 stars"></label>
        <input type="radio" id="star4half_2" name="rating_2" value="4 and a half" /><label class="half" for="star4half_2" title="Pretty good - 4.5 stars"></label>
        <input type="radio" id="star4_2" name="rating_2" value="4" /><label class = "full" for="star4_2" title="Pretty good - 4 stars"></label>
        <input type="radio" id="star3half_2" name="rating_2" value="3 and a half" /><label class="half" for="star3half_2" title="Meh - 3.5 stars"></label>
        <input type="radio" id="star3_2" name="rating_2" value="3" /><label class = "full" for="star3_2" title="Meh - 3 stars"></label>
        <input type="radio" id="star2half_2" name="rating_2" value="2 and a half" /><label class="half" for="star2half_2" title="Kinda bad - 2.5 stars"></label>
        <input type="radio" id="star2_2" name="rating_2" value="2" /><label class = "full" for="star2_2" title="Kinda bad - 2 stars"></label>
        <input type="radio" id="star1half_2" name="rating_2" value="1 and a half" /><label class="half" for="star1half_2" title="Meh - 1.5 stars"></label>
        <input type="radio" id="star1_2" name="rating_2" value="1" /><label class = "full" for="star1_2" title="Sucks big time - 1 star"></label>
        <input type="radio" id="starhalf_2" name="rating_2" value="half" /><label class="half" for="starhalf_2" title="Sucks big time - 0.5 stars"></label>
    </fieldset>