dqellle310167 2015-05-25 20:00
浏览 30
已采纳

带有复选框和PHP后端的星级评级系统

I want to make a star rating system in html with checkboxes. When a checkbox is checked, the previous are checked and the others are unchecked.

The result will be POSTed to my php code backend.

Here's my current html:

<span class="star-rating">
   <!--RADIO 1-->
    <input type='checkbox' class="radio_item" value="1" name="item" id="radio1">
        <label class="label_item" for="radio1"> <img src="label.png" style="width:30px;height:28px"> </label>

    <!--RADIO 2-->
    <input type='checkbox' class="radio_item" value="2" name="item2" id="radio2">
    <label class="label_item" for="radio2"> <img src="label.png" style="width:30px;height:28px"> </label>

      <!--RADIO 3-->
    <input type='checkbox' class="radio_item" value="3" name="item3" id="radio3">
    <label class="label_item" for="radio3"> <img src="label.png" style="width:30px;height:28px"> </label>


      <!--RADIO 4-->
    <input type='checkbox' class="radio_item" value="4" name="item4" id="radio4">
    <label class="label_item" for="radio4"> <img src="label.png" style="width:30px;height:28px"> </label>

      <!--RADIO 5-->
    <input type='checkbox' class="radio_item" value="5" name="item5" id="radio5">
    <label class="label_item" for="radio5"> <img src="label.png" style="width:30px;height:28px"> </label>
</span>
  • 写回答

1条回答 默认 最新

  • dongyuan8312 2015-05-25 20:31
    关注

    Include Jquery in your html and this simple javascript code might do just what you want.

    $('.star-rating input').click( function(){
        starvalue = $(this).attr('value');
        
        // iterate through the checkboxes and check those with values lower than or equal to the one you selected. Uncheck any other.
        for(i=0; i<=5; i++){
            if (i <= starvalue){
                $("#radio" + i).prop('checked', true);
            } else {
                $("#radio" + i).prop('checked', false);
            }
        }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <span class="star-rating">
       <!--RADIO 1-->
        <input type='checkbox' class="radio_item" value="1" name="item" id="radio1">
            <label class="label_item" for="radio1"> &#9734 </label>
    
        <!--RADIO 2-->
        <input type='checkbox' class="radio_item" value="2" name="item2" id="radio2">
        <label class="label_item" for="radio2"> &#9734 </label>
    
          <!--RADIO 3-->
        <input type='checkbox' class="radio_item" value="3" name="item3" id="radio3">
        <label class="label_item" for="radio3"> &#9734 </label>
    
    
          <!--RADIO 4-->
        <input type='checkbox' class="radio_item" value="4" name="item4" id="radio4">
        <label class="label_item" for="radio4"> &#9734 </label>
    
          <!--RADIO 5-->
        <input type='checkbox' class="radio_item" value="5" name="item5" id="radio5">
        <label class="label_item" for="radio5"> &#9734 </label>
    </span>

    Obs: The html code is the same as yours. I've just replaced the images by stars as the links were broken.

    Obs2: When you post this to your php, you will receive all checked inputs. Your php code will have to be smart and take the highest value it receives. This should not be difficult.

    Obs3: The stars should behave as radio-buttons and not checkboxes. The workaround from Obs2 means that your backend code has too much knowledge of what is happening on the interface. This is a more advanced tip, but take that in consideration in the future.

    EXTRA

    To include this code in your app, you have some options:

    OPTION 1 (jQuery from google CDN and javascript code on script tag)

    Put this in your html:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script>
    $('.star-rating input').click( function(){
            starvalue = $(this).attr('value');
    
            // iterate through the checkboxes and check those with values lower than or equal to the one you selected. Uncheck any other.
            for(i=0; i<=5; i++){
                if (i <= starvalue){
                    $("#radio" + i).prop('checked', true);
                } else {
                    $("#radio" + i).prop('checked', false);
                }
            }
        });
    </script>
    

    OPTION 2 (better: jQuery from CDN and javascript file included with php):

    Include jQuery as above and put the javascript code in a file: star_rating.js, then include that file with php include command.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)