我已经按照这个很棒的教程来实现星级评分系统: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({
数字:函数(){
return $(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"><div></div></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/