将 CSS 星级评定集成到 HTML 表单中
我在网上看过一个教程 - http://www.htmldrive .net/items/show/402/CSS-Star-Rating-System - 用于制作 CSS 星级评级系统。
他们的页面上有这样的代码:
<ul class="rating">
<li><a href="#" title="1 Star">1</a></li>
<li><a href="#" title="2 Stars">2</a></li>
<li><a href="#" title="3 Stars">3</a></li>
<li><a href="#" title="4 Stars">4</a></li>
<li><a href="#" title="5 Stars">5</a></li>
</ul>
据我所知,这只是一个列表。我如何将其集成到我的表单中,以便将星级信息提交到数据库。我当前的表单代码如下:
<p>Quality</p>
<input type="radio" name="ratingquality" value="1">
<input type="radio" name="ratingquality" value="2">
<input type="radio" name="ratingquality" value="3">
<input type="radio" name="ratingquality" value="4">
<input type="radio" name="ratingquality" value="5">
I've seen a tutorial online - http://www.htmldrive.net/items/show/402/CSS-Star-Rating-System - for making a CSS star rating system.
On the page they have this code:
<ul class="rating">
<li><a href="#" title="1 Star">1</a></li>
<li><a href="#" title="2 Stars">2</a></li>
<li><a href="#" title="3 Stars">3</a></li>
<li><a href="#" title="4 Stars">4</a></li>
<li><a href="#" title="5 Stars">5</a></li>
</ul>
From what I can tell, this is just a list. How would I integrate it into my form, so that the information from the star rating can be submitted to a database. My current code for the form is below:
<p>Quality</p>
<input type="radio" name="ratingquality" value="1">
<input type="radio" name="ratingquality" value="2">
<input type="radio" name="ratingquality" value="3">
<input type="radio" name="ratingquality" value="4">
<input type="radio" name="ratingquality" value="5">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这非常容易使用,只需复制粘贴代码即可。您可以在背景中使用自己的星星图像。
我创建了一个变量
var userRating
。您可以使用此变量从星星中获取价值。享受!! :)
This is very easy to use, just copy-paste the code. You can use your own star image in background.
I have created a variable
var userRating
. you can use this variable to get value from stars.Enjoy!! :)
以下是如何在不使用 javascript 的情况下将 CSS 星级评级集成到 HTML 表单中(仅 html 和 css):
CSS:
HTML:
请检查 演示
Here is how to integrate CSS star rating into an HTML form without using javascript (only html and css):
CSS:
HTML:
Please check the demo
这个怎么样?我需要完全相同的东西,我必须从头开始创建一个。它是纯 CSS,可在 IE9+ 中运行,请随意对其进行改进。
演示:http://www.dkj.com/Articles/Web_Development/Pure_CSS_5_Star_Rating_System_with_Radios/
CSS:
How about this? I needed the exact same thing, I had to create one from scratch. It's PURE CSS, and works in IE9+ Feel-free to improve upon it.
Demo: http://www.d-k-j.com/Articles/Web_Development/Pure_CSS_5_Star_Rating_System_with_Radios/
CSS:
CSS:
HTML:
CSS:
HTML:
我马上要说的是,您将无法通过严格使用 CSS 的单选按钮来实现它们的外观。
但是,您可以坚持使用您发布的示例中的列表样式,并将
anchors
替换为可点击的spans
,这将触发一个 javascript 事件,进而将该评级保存到你的数据库通过ajax。如果您选择了这条路线,您可能还想将 cookie 保存到用户计算机上,这样他们就无法一次又一次地提交到您的数据库。这将阻止他们多次提交,至少在他们删除 cookie 之前是如此。
但当然有很多方法可以解决这个问题。这只是其中之一。希望有帮助。
I'm going to say right off the bat that you will not be able to achieve the look they have with radio buttons with strictly CSS.
You could, however, stick to the list style in the example you posted and replace the
anchors
with clickablespans
that would trigger a javascript event that would in turn save that rating to your database via ajax.If you went that route you would probably also want to save a cookie to the users machine so that they could not submit over and over again to your database. That would prevent them from submitting more than once at least until they deleted their cookies.
But of course there are many ways to address this problem. This is just one of them. Hope that helps.
这是解决方案。
HTML:
CSS:
希望这会有所帮助。
来源
Here is the solution.
The HTML:
The CSS:
Hope this helps.
Source