html - 显示值中的星号

标签 html css

我按照这个很棒的教程来实现星级评级系统:Tutorial Demo

这是完全有效的,我已经完成了将星星表格保存到 MySQL 表中的必要工作。 现在我想用教程中的星星系统显示在我的表中注册的星星值。示例 $starsvalue = 4; 然后它显示 4 颗彩色星星,最后一颗是空的。

目前我不知道如何用 $starsvalue 的结果显示星星。非常欢迎任何帮助。

使用的表格:

echo "<form id=\"ratingsForm\" name=\"starsrating\" method=\"POST\">";
                echo "<div class=\"stars\">";
                echo "<input type=\"radio\" name=\"star\" class=\"star-1\" id=\"star-1\" value=\"1\" onclick=\"this.form.submit()\"/>";
                echo "<label class=\"star-1\" for=\"star-1\">1</label>";
                echo "<input type=\"radio\" name=\"star\" class=\"star-2\" id=\"star-2\" value=\"2\" onclick=\"this.form.submit()\"/>";
                echo "<label class=\"star-2\" for=\"star-2\">2</label>";
                echo "<input type=\"radio\" name=\"star\" class=\"star-3\" id=\"star-3\" value=\"3\" onclick=\"this.form.submit()\"/>";
                echo "<label class=\"star-3\" for=\"star-3\">3</label>";
                echo "<input type=\"radio\" name=\"star\" class=\"star-4\" id=\"star-4\" value=\"4\" onclick=\"this.form.submit()\"/>";
                echo "<label class=\"star-4\" for=\"star-4\">4</label>";
                echo "<input type=\"radio\" name=\"star\" class=\"star-5\" id=\"star-5\" value=\"5\" onclick=\"this.form.submit()\"/>";
                echo "<label class=\"star-5\" for=\"star-5\">5</label>";
                echo "<span></span>";
                echo "</div>";

                echo "<div style=\"visibility:hidden\"><Input type=submit name=\"choix\" value=\"Valider\"></div>";

                echo "</form>";

使用的 CSS:

   body {
      padding: 50px;
    }
    form .stars {
      background: url("stars.png") repeat-x 0 0;
      width: 150px;
      margin: 0 auto;
    }

    form .stars input[type="radio"] {
      position: absolute;
      opacity: 0;
      filter: alpha(opacity=0);
    }
    form .stars input[type="radio"].star-5:checked ~ span {
      width: 100%;
    }
    form .stars input[type="radio"].star-4:checked ~ span {
      width: 80%;
    }
    form .stars input[type="radio"].star-3:checked ~ span {
      width: 60%;
    }
    form .stars input[type="radio"].star-2:checked ~ span {
      width: 40%;
    }
    form .stars input[type="radio"].star-1:checked ~ span {
      width: 20%;
    }
    form .stars label {
      display: block;
      width: 30px;
      height: 30px;
      margin: 0!important;
      padding: 0!important;
      text-indent: -999em;
      float: left;
      position: relative;
      z-index: 10;
      background: transparent!important;
      cursor: pointer;
    }
    form .stars label:hover ~ span {
      background-position: 0 -30px;
    }
    form .stars label.star-5:hover ~ span {
      width: 100% !important;
    }
    form .stars label.star-4:hover ~ span {
      width: 80% !important;
    }
    form .stars label.star-3:hover ~ span {
      width: 60% !important;
    }
    form .stars label.star-2:hover ~ span {
      width: 40% !important;
    }
    form .stars label.star-1:hover ~ span {
      width: 20% !important;
    }
    form .stars span {
      display: block;
      width: 0;
      position: relative;
      top: 0;
      left: 0;
      height: 30px;
      background: url("stars.png") repeat-x 0 -60px;
      -webkit-transition: -webkit-width 0.5s;
      -moz-transition: -moz-width 0.5s;
      -ms-transition: -ms-width 0.5s;
      -o-transition: -o-width 0.5s;
      transition: width 0.5s;
    }

* 更新 *

按照建议尝试 Raty,但我无法让星星出现。 CSS 和 JS 文件似乎在好的地方。我想我不明白如何配置它,比如设置星星图像文件夹和其他参数

我不知道应该在哪里复制那些在 raty 网页上很常见的代码?

$('div').raty({ 数字:函数(){ 返回 $(this).attr('data-number'); } });

我的代码:

  <link rel="stylesheet" href="./raty/lib/jquery.raty.css"/>
    <link rel="stylesheet" href="./raty/stylesheets/labs.css" media="screen" type="text/css"/>
    <script src="./raty/vendor/jquery.js"></script><script src="./raty/lib/jquery.raty.js"></script>
    <script src="./raty/javascripts/labs.js" type="text/javascript"></script>

    <div class="clearfix"></div>

<article>

    <div class="body">
        <h1>Default</h1>

        <p>You need just to have a <code>div</code> to build the Raty.</p>

        <div id="default"></div>
        <div class="highlight"><pre><span class="nt">&lt;div&gt;&lt;/div&gt;</span>
        </pre></div>
        <div class="highlight"><pre><span class="nx">$</span><span class="p">(</span><span class="s1">'div'</span><span class="p">).</span><span class="nx">raty</span><span class="p">();</span>
        </pre></div>    

最佳答案

根据教程,如果单选按钮被选中,那么您将看到黄色的星星。

$starsvalue=4;
echo "<form id=\"ratingsForm\" name=\"starsrating\" method=\"POST\">";
echo "<div class=\"stars\">";
for ($i = 1; $i <= 5; $i++) {
    echo "<input type=\"radio\" name=\"star\" class=\"star-$i\" id=\"star-$i\" value=\"$i\" ";
    if ($i === $starsvalue) {
        echo " checked='checked' ";
    }
    echo "onclick=\"this.form.submit()\"/>";
    echo "<label class=\"star-$i\" for=\"star-$i\">$i</label>";
}
echo "<span></span>";
echo "</div>";

echo "<div style=\"visibility:hidden\"><Input type=submit name=\"choix\" value=\"Valider\"></div>";

echo "</form>";

关于html - 显示值中的星号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30076869/

相关文章:

javascript - 在 Flask 中显示加载/启动页面

javascript - 无法使用 Angular 获取 HTML textarea 输入文本

javascript - CSS3 旋转延迟

html - 没有高度、边距、填充或边框的元素仍然占据空间,因为它下面的元素有边距

css - 如何使用响应式字母间距悬停动画将文本定位在 HTML 页面的四个侧面

html - UIWebView 未检测到分页符

javascript - Angularjs 进度圈不适用于动态输入

javascript - 如何在 Django 博客中添加社交分享按钮

javascript - JSSOR slider 不可见

html - 在 twig/volt 中包含 .css 文件