【问题标题】:How to create a list rating post by radio button?如何通过单选按钮创建列表评分帖子?
【发布时间】:2019-10-09 07:07:32
【问题描述】:

我创建了一个课程列表。每节课旁边都有十颗星来评价这节课。一切都很好,但似乎 css 是错误的。例如,当我点击星星为第三行的课程评分时,css 将悬停在第一行。

我通过这样的单选按钮创建列表评级。 我的看法

<tbody>
@foreach($rates as $rate)
   <form method="post" action="{{url('/rating')}}">
      <input type="hidden" name="rateId" value="{{$rate->id}}">
      {{ csrf_field() }}
         <tr style="display: flex;">
              <td style="width:100px;"></td>
              <td style="text-align: left; width:400px;"><a href="{{url('/bai-giang/'.$rate->id)}}">{{$rate->title}}</a></td>
              <td style="text-align: center; width:200px;"></td>
              <td style="width:300px;">
                 <div align="center" style="display: inline-flex;">
                     <fieldset class="rating">
                        <input type="radio" id="star10" name="rating" value="10" /><label for="star10" title="Rocks!">10 stars</label>
                        <input type="radio" id="star9" name="rating" value="9" /><label for="star9" title="Rocks!">9 stars</label>
                        <input type="radio" id="star8" name="rating" value="8" /><label for="star8" title="Rocks!">8 stars</label>
                        <input type="radio" id="star7" name="rating" value="7" /><label for="star7" title="Rocks!">7 stars</label>
                        <input type="radio" id="star6" name="rating" value="6" /><label for="star6" title="Rocks!">6 stars</label>
                        <input type="radio" id="star5" name="rating" value="5" /><label for="star5" title="Rocks!">5 stars</label>
                        <input type="radio" id="star4" name="rating" value="4" /><label for="star4" title="Rocks!">4 stars</label>
                        <input type="radio" id="star3" name="rating" value="3" /><label for="star3" title="Rocks!">3 stars</label>
                        <input type="radio" id="star2" name="rating" value="2" /><label for="star2" title="Rocks!">2 stars</label>
                        <input type="radio" id="star1" name="rating" value="1" /><label for="star1" title="Rocks!">1 stars</label>
                   </fieldset>
                 </div>
              </td>
              <th style="width:100px;">
                  <button type="submit" class="btn btn-link" onclick="return confirmAction()"> Đánh giá</button>
               </th> 
          </tr>
  </form>
@endforeach
</tbody>

单选按钮的 CSS


    .rating {
        float:left;
    }

    .rating:not(:checked) > input {
            position: absolute;
            /* top: -9999px; */
            clip: rect(0, 0, 0, 0);
            height: 0;
            width: 0;
            overflow: hidden;
            opacity: 0;
    }

    .rating:not(:checked) > label {
        float:right;
        width:1em;
        padding:0 .1em;
        overflow:hidden;
        white-space:nowrap;
        cursor:pointer;
        font-size:200%;
        line-height:1.2;
        color:#ddd;
        text-shadow:1px 1px #bbb, 2px 2px #666, .1em .1em .2em rgba(0,0,0,.5);
    }

    .rating:not(:checked) > label:before {
        content: '★ ';
    }

    .rating > input:checked ~ label {
        color: #f70;
        text-shadow:1px 1px #c60, 2px 2px #940, .1em .1em .2em rgba(0,0,0,.5);
    }

    .rating:not(:checked) > label:hover,
    .rating:not(:checked) > label:hover ~ label {
        color: gold;
        text-shadow:1px 1px goldenrod, 2px 2px #B57340, .1em .1em .2em rgba(0,0,0,.5);
    }

    .rating > input:checked + label:hover,
    .rating > input:checked + label:hover ~ label,
    .rating > input:checked ~ label:hover,
    .rating > input:checked ~ label:hover ~ label,
    .rating > label:hover ~ input:checked ~ label {
        color: #ea0;
        text-shadow:1px 1px goldenrod, 2px 2px #B57340, .1em .1em .2em rgba(0,0,0,.5);
    }

    .rating > label:active {
        position:relative;
        top:2px;
        left:2px;
    }

我的结果:当我点击第三行的星星时,它会像这样悬停在第一行

【问题讨论】:

    标签: php css laravel


    【解决方案1】:

    纯 CSS 解决方案可能会对您有所帮助

    HTML

        <tbody>
    @foreach($rates as $rate)
       <form method="post" action="{{url('/rating')}}">
          <input type="hidden" name="rateId" value="{{$rate->id}}">
          {{ csrf_field() }}
             <tr style="display: flex;">
                  <td style="width:100px;"></td>
                  <td style="text-align: left; width:400px;"><a href="{{url('/bai-giang/'.$rate->id)}}">{{$rate->title}}</a></td>
                  <td style="text-align: center; width:200px;"></td>
                  <td style="width:300px;">
                     <div align="center" style="display: inline-flex;">
                         <fieldset class="rating">
                            <input type="radio" id="star10" name="rating" value="10" /><label for="star10" title="Rocks!">10 stars</label>
                            <input type="radio" id="star9" name="rating" value="9" /><label for="star9" title="Rocks!">9 stars</label>
                            <input type="radio" id="star8" name="rating" value="8" /><label for="star8" title="Rocks!">8 stars</label>
                            <input type="radio" id="star7" name="rating" value="7" /><label for="star7" title="Rocks!">7 stars</label>
                            <input type="radio" id="star6" name="rating" value="6" /><label for="star6" title="Rocks!">6 stars</label>
                            <input type="radio" id="star5" name="rating" value="5" /><label for="star5" title="Rocks!">5 stars</label>
                            <input type="radio" id="star4" name="rating" value="4" /><label for="star4" title="Rocks!">4 stars</label>
                            <input type="radio" id="star3" name="rating" value="3" /><label for="star3" title="Rocks!">3 stars</label>
                            <input type="radio" id="star2" name="rating" value="2" /><label for="star2" title="Rocks!">2 stars</label>
                            <input type="radio" id="star1" name="rating" value="1" /><label for="star1" title="Rocks!">1 stars</label>
                       </fieldset>
                     </div>
                  </td>
                  <th style="width:100px;">
                      <button type="submit" class="btn btn-link" onclick="return confirmAction()"> Đánh giá</button>
                   </th> 
              </tr>
         <tr style="display: flex;">
                  <td style="width:100px;"></td>
                  <td style="text-align: left; width:400px;"><a href="{{url('/bai-giang/'.$rate->id)}}">{{$rate->title}}</a></td>
                  <td style="text-align: center; width:200px;"></td>
                  <td style="width:300px;">
                     <div align="center" style="display: inline-flex;">
                         <fieldset class="rating">
                            <input type="radio" id="star101" name="rating1" value="10" /><label for="star101" title="Rocks!">10 stars</label>
                            <input type="radio" id="star91" name="rating1" value="9" /><label for="star91" title="Rocks!">9 stars</label>
                            <input type="radio" id="star81" name="rating1" value="8" /><label for="star81" title="Rocks!">8 stars</label>
                            <input type="radio" id="star71" name="rating1" value="7" /><label for="star71" title="Rocks!">7 stars</label>
                            <input type="radio" id="star61" name="rating1" value="6" /><label for="star61" title="Rocks!">6 stars</label>
                            <input type="radio" id="star51" name="rating1" value="5" /><label for="star51" title="Rocks!">5 stars</label>
                            <input type="radio" id="star41" name="rating1" value="4" /><label for="star41" title="Rocks!">4 stars</label>
                            <input type="radio" id="star31" name="rating1" value="3" /><label for="star31" title="Rocks!">3 stars</label>
                            <input type="radio" id="star21" name="rating1" value="2" /><label for="star21" title="Rocks!">2 stars</label>
                            <input type="radio" id="star11" name="rating1" value="1" /><label for="star11" title="Rocks!">1 stars</label>
                       </fieldset>
                     </div>
                  </td>
                  <th style="width:100px;">
                      <button type="submit" class="btn btn-link" onclick="return confirmAction()"> Đánh giá</button>
                   </th> 
              </tr>
      </form>
    @endforeach
    </tbody>
    

    CSS

    .rating {
        float:left;
    }
    
    .rating:not(:checked) > input {
            position: absolute;
            /* top: -9999px; */
            clip: rect(0, 0, 0, 0);
            height: 0;
            width: 0;
            overflow: hidden;
            opacity: 0;
    }
    
    .rating:not(:checked) > label {
        float:right;
        width:1em;
        padding:0 .1em;
        overflow:hidden;
        white-space:nowrap;
        cursor:pointer;
        font-size:200%;
        line-height:1.2;
        color:#ddd;
        text-shadow:1px 1px #bbb, 2px 2px #666, .1em .1em .2em rgba(0,0,0,.5);
    }
    
    .rating:not(:checked) > label:before {
        content: '★ ';
    }
    
    .rating > input:checked ~ label {
        color: #f70;
        text-shadow:1px 1px #c60, 2px 2px #940, .1em .1em .2em rgba(0,0,0,.5);
    }
    
    .rating:not(:checked) > label:hover,
    .rating:not(:checked) > label:hover ~ label {
        color: gold;
        text-shadow:1px 1px goldenrod, 2px 2px #B57340, .1em .1em .2em rgba(0,0,0,.5);
    }
    
    .rating > input:checked + label:hover,
    .rating > input:checked + label:hover ~ label,
    .rating > input:checked ~ label:hover,
    .rating > input:checked ~ label:hover ~ label,
    .rating > label:hover ~ input:checked ~ label {
        color: #ea0;
        text-shadow:1px 1px goldenrod, 2px 2px #B57340, .1em .1em .2em rgba(0,0,0,.5);
    }
    
    .rating > label:active {
        position:relative;
        top:2px;
        left:2px;
    }
    

    【讨论】:

    • 它和我的错误一样。当我点击第三行上的星星时,它会在第一行上悬停星星
    • 你改了id吗?因为它们是独一无二的
    • 更改第二组的名称,对其进行评分
    • 同理,貌似只在第一行有效
    • 你确定你也改变了评级组的名字和ID吗?
    猜你喜欢
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    • 2011-10-21
    相关资源
    最近更新 更多