我正在尝试建立一个星级评定系统,我有此代码,我想对其进行一些更改,当用户单击星星后,它会显示一个警告,其中显示有多少颗星星,并且会重置颜色,我want是用户单击后保留的颜色填充,然后将警报替换为星空下的div,这是我的代码:
Js:
$(function() {
$("div.star-rating > s, div.star-rating-rtl > s").on("click", function(e) {
// remove all active classes first, needed if user clicks multiple times
$(this).closest('div').find('.active').removeClass('active');
$(e.target).parentsUntil("div").addClass('active'); // all elements up from the clicked one excluding self
$(e.target).addClass('active'); // the element user has clicked on
var numStars = $(e.target).parentsUntil("div").length+1;
$('.show-result').text(numStars + (numStars == 1 ? " star" : " stars!"));
});
});
CSS:
.show-result {
margin: 10px;
padding: 10px;
color: green;
font-size: 20px;
}
.star-rating s:hover,
.star-rating s.active {
color: red;
}
.star-rating-rtl s:hover,
.star-rating-rtl s.active {
color: red;
}
.star-rating s,
.star-rating-rtl s {
color: black;
font-size: 50px;
cursor: default;
text-decoration: none;
line-height: 50px;
}
.star-rating {
padding: 2px;
}
.star-rating-rtl {
background: #555;
display: inline-block;
border: 2px solid #444;
}
.star-rating-rtl s {
color: yellow;
}
.star-rating s:hover:before,
.star-rating s.rated:before,
.star-rating s.active:before {
content: "\2605";
}
.star-rating s:before {
content: "\2606";
}
.star-rating-rtl s:hover:after,
.star-rating-rtl s.rated:after,
.star-rating-rtl s.active:after {
content: "\2605";
}
.star-rating-rtl s:after {
content: "\2606";
}
html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="star-rating"><s><s><s><s><s></s></s></s></s></s></div>
<div class="show-result">No stars selected yet.</div>
这里可以显示值,但是如何在mysqli中存储该值。请帮助我
要在数据库中存储星级,应使用Ajax。下面的代码显示了使用Jquery Ajax发布将值发布到服务器端脚本(PHP)。
$(e.target).addClass('active'); // the element user has clicked on
var numStars = $(e.target).parentsUntil("div").length+1;
// modification starts here....
$.post( "saveRatings.php", { rating: numStars})// you may pass other necessary information
.done(function( data ) {
$('.show-result').text(numStars + (numStars == 1 ? " star" : " stars!"));
});
在您的saveRatings.php
文件中,
$ratings = $_POST['ratings'];
// You may need to pass more information to identify the user who gives the rating or the product that is being rated.
// Add your MySQL query to save the rating information to a database table
// return a success/failure response as needed.
您可能需要从数据库中获取等级并将其传递给javascript代码,以在整个页面重新加载期间保持等级。