多个星级评分问题

时间:2017-05-05 06:14:05

标签: jquery html forms

我有一个星级应用程序,用户可以在1到5之间投票。但现在它只允许单一评级,即如果我从第二个选择星号,第一个部分似乎是选择而不是第二个(总是默认第一部分)。我试图改变课程和身份,但它没有用。

Html代码:

<fieldset id='demo1' class="rating">
   <input class="stars" type="radio" id="star_a-5" name="rating" value="5" />
   <label class = "full" for="star5" title="Awesome - 5 stars"></label>
   <input class="stars" type="radio" id="star_a_5-half" name="rating" value="4.5" />
   <label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
   <input class="stars" type="radio" id="star_a-4" name="rating" value="4" />
   <label class = "full" for="star4" title="Pretty good - 4 stars"></label>
   <input class="stars" type="radio" id="star_a_4-half" name="rating" value="3.5" />
   <label class="half" for="star3half" title="Meh - 3.5 stars"></label>
   <input class="stars" type="radio" id="star_a-3" name="rating" value="3" />
   <label class = "full" for="star3" title="Meh - 3 stars"></label>
   <input class="stars" type="radio" id="star_a_3-half" name="rating" value="2.5" />
   <label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>
   <input class="stars" type="radio" id="star2" name="rating" value="2" />
   <label class = "full" for="star2" title="Kinda bad - 2 stars"></label>
   <input class="stars" type="radio" id="star1half" name="rating" value="1.5" />
   <label class="half" for="star1half" title="Meh - 1.5 stars"></label>
   <input class="stars" type="radio" id="star1" name="rating" value="1" />
   <label class = "full" for="star1" title="Sucks big time - 1 star"></label>
   <input class="stars" type="radio" id="starhalf" name="rating" value="0.5" />
   <label class="half" for="starhalf" title="Sucks big time - 0.5 stars"></label>
</fieldset>    
</div>
<br><br><br>
<p><strong>2. Productivity </strong>- the extent to which the employee's behaviors are directed toward fostering positive
                working relationships in a diverse workplace, respect for one's fellow workers, and cooperation with students,
                customers, and visitors. </p>
<div class="form-group">
   <label for="comment">Comments:</label>
   <textarea class="form-control" rows="5" id="comment"></textarea>
</div>
<div class="form-group">
   <fieldset id='demo2' class="rating">
      <input class="stars" type="radio" id="star5" name="rating" value="5" />
      <label class = "full" for="star5" title="Awesome - 5 stars"></label>
      <input class="stars" type="radio" id="star4half" name="rating" value="4.5" />
      <label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
      <input class="stars" type="radio" id="star4" name="rating" value="4" />
      <label class = "full" for="star4" title="Pretty good - 4 stars"></label>
      <input class="stars" type="radio" id="star3half" name="rating" value="3.5" />
      <label class="half" for="star3half" title="Meh - 3.5 stars"></label>
      <input class="stars" type="radio" id="star3" name="rating" value="3" />
      <label class = "full" for="star3" title="Meh - 3 stars"></label>
      <input class="stars" type="radio" id="star2half" name="rating" value="2.5" />
      <label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>
      <input class="stars" type="radio" id="star2" name="rating" value="2" />
      <label class = "full" for="star2" title="Kinda bad - 2 stars"></label>
      <input class="stars" type="radio" id="star1half" name="rating" value="1.5" />
      <label class="half" for="star1half" title="Meh - 1.5 stars"></label>
      <input class="stars" type="radio" id="star1" name="rating" value="1" />
      <label class = "full" for="star1" title="Sucks big time - 1 star"></label>
      <input class="stars" type="radio" id="starhalf" name="rating" value="0.5" />
      <label class="half" for="starhalf" title="Sucks big time - 0.5 stars"></label>
   </fieldset>    
</div>
<br><br><br> 
<p><strong>3. Work Quality </strong> - the extent to which the employee contributes to the effectiveness of the
                department and the overall mission of the university. (NOTE: Time off approved under FMLA may not be
                considered)  </p>
<div class="form-group">
   <label for="comment">Comments: :</label>
   <textarea class="form-control" rows="5" id="comment"></textarea>
</div>
<div class="form-group">
   <fieldset id='demo3' class="rating">
      <input class="stars" type="radio" id="star5" name="rating" value="5" />
      <label class = "full" for="star5" title="Awesome - 5 stars"></label>
      <input class="stars" type="radio" id="star4half" name="rating" value="4.5" />
      <label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
      <input class="stars" type="radio" id="star4" name="rating" value="4" />
      <label class = "full" for="star4" title="Pretty good - 4 stars"></label>
      <input class="stars" type="radio" id="star3half" name="rating" value="3.5" />
      <label class="half" for="star3half" title="Meh - 3.5 stars"></label>
      <input class="stars" type="radio" id="star3" name="rating" value="3" />
      <label class = "full" for="star3" title="Meh - 3 stars"></label>
      <input class="stars" type="radio" id="star2half" name="rating" value="2.5" />
      <label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>
      <input class="stars" type="radio" id="star2" name="rating" value="2" />
      <label class = "full" for="star2" title="Kinda bad - 2 stars"></label>
      <input class="stars" type="radio" id="star1half" name="rating" value="1.5" />
      <label class="half" for="star1half" title="Meh - 1.5 stars"></label>
      <input class="stars" type="radio" id="star1" name="rating" value="1" />
      <label class = "full" for="star1" title="Sucks big time - 1 star"></label>
      <input class="stars" type="radio" id="starhalf" name="rating" value="0.5" />
      <label class="half" for="starhalf" title="Sucks big time - 0.5 stars"></label>
   </fieldset>    
</div>
<br><br><br>

css:

 <style>
    /****** Rating Starts *****/
    @import url(http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css);

    fieldset, label { margin: 0; padding: 0; }
    body{ margin: 20px; }
    h1 { font-size: 1.5em; margin: 10px; }
    .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; 
    }
    .rating > input:checked ~ label, 
       .rating:not(:checked) > label:hover,  
       .rating:not(:checked) > label:hover ~ label { color: #FFD700;
    }
    .rating > input:checked + label:hover, 
       .rating > input:checked ~ label:hover,
       .rating > label:hover ~ input:checked ~ label, 
       .rating > input:checked ~ label:hover ~ label { color: #FFED85;
    }     
    /* Downloaded from http://devzone.co.in/ */
</style>

JS:

<script>
   $('document').ready(function () {
      $('.toggleswitch').bootstrapToggle();
      $("#demo1 .stars").click(function () { 
         alert($(this).val());
         $(this).attr("checked");
      });
      $("#demo2 .stars").click(function () { 
         alert($(this).val());
         $(this).attr("checked");
      });
      $("#demo3.stars").click(function () { 
         alert($(this).val());
         $(this).attr("checked");
      });
   });
</script>

请帮帮我。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

HTML中存在多个问题:

  1. 您有多个具有相同id的元素,页面上的id永远不会超过1个元素。
  2. 您的所有radio's都具有相同的name属性值。因此,如果您点击第一个评级,它将删除评级2或3的任何评级
  3. 我还更改了您的jQuery以使其更简单。基本上我改变了这一点:$("fieldset[id^='demo'] .stars")现在所有fieldset的{​​{1}}都以id

    开头

    &#13;
    &#13;
    demo
    &#13;
    $('document').ready(function() {
      $('.toggleswitch').bootstrapToggle();
      $("fieldset[id^='demo'] .stars").click(function() {
        alert($(this).val());
        $(this).attr("checked");
      });
    });
    &#13;
    /****** Rating Starts *****/
    
    @import url(http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css);
    fieldset,
    label {
      margin: 0;
      padding: 0;
    }
    
    body {
      margin: 20px;
    }
    
    h1 {
      font-size: 1.5em;
      margin: 10px;
    }
    
    .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;
    }
    
    .rating>input:checked~label,
    .rating:not(:checked)>label:hover,
    .rating:not(:checked)>label:hover~label {
      color: #FFD700;
    }
    
    .rating>input:checked+label:hover,
    .rating>input:checked~label:hover,
    .rating>label:hover~input:checked~label,
    .rating>input:checked~label:hover~label {
      color: #FFED85;
    }
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

你的一些ids正在重复。重命名它。 在demo1中检查id =“star2half”

<input class="stars" type="radio" id="star2half" name="rating" value="2.5" />
<label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>

演示3:

<input class="stars" type="radio" id="star2half" name="rating" value="2.5" />
<label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>