【问题标题】:Multiple Star Review on Same Page同一页面上的多星评论
【发布时间】:2015-09-08 14:10:25
【问题描述】:

我有一个多页表单,在其中一个页面上我有一个星级评论应用程序,用户可以在其中投票 1 到 5 颗星。我的效果很好,但是在同一页面上我需要多次(即要查看不同的内容)。

我尝试了很多方法;更改 'stars' 和 'star' 的类别,但都不允许多个评级 - 它们都触发回第一个(即,如果您在第二个上选择 2 星,它也会默认第一个选择为 2 星,或者只选择第一次选择 2 星)。有什么想法吗?

HTML 代码:

<div class="stars">
    <input type="radio" name="star" class="star-1" id="star-1" />
    <label class="star-1" for="star-1">1</label>
    <input type="radio" name="star" class="star-2" id="star-2" />
    <label class="star-2" for="star-2">2</label>
    <input type="radio" name="star" class="star-3" id="star-3" />
    <label class="star-3" for="star-3">3</label>
    <input type="radio" name="star" class="star-4" id="star-4" />
    <label class="star-4" for="star-4">4</label>
    <input type="radio" name="star" class="star-5" id="star-5" />
    <label class="star-5" for="star-5">5</label>
    <span></span>
</div>

CSS:

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;
}

【问题讨论】:

  • name 元素中的 input 属性对于每条评论都必须是唯一的。它是包含值的变量。您的后端并不神奇地知道哪条评论发布了价值。
  • 所以每一个都改变“name=____”?我很确定我已经尝试过了,但没有任何区别。不过我会再试一次。
  • 嗯,还有更多。但是没有你的后端代码,我就帮不了你了。
  • “后端”代码是什么意思?这是我获取代码的地方,仅此而已:voidcanvas.com/…
  • PHP 代码。实际保存收视率的部分?如果评分没有保存在任何地方,那么在您的网站上设置评分有什么意义?

标签: php html css forms


【解决方案1】:

我通过更改无线电名称并保持 ID 唯一来实现它。

单选按钮按名称分组,以便每个按钮代表特定形式“变量”的值。每组星代表一个不同的变量,一个产品的评级,每个组都应该有一个单独的名称来反映他们所针对的产品,例如。附加产品 ID,name="star-123456"。您还可以在表单的项目列表中附加产品的索引。

ID 在元素之间应该始终是唯一的。在这种情况下,单选输入 id 应该是唯一的,以便标签的 for 属性指向正确的。

下面的 sn-p 包含两个无线电组的唯一名称和每个无线电输入的唯一 ID。

form .stars {
  background: gray;
  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: 28px;
  height: 28px;
  border: 1px solid black;
  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: red;
  filter: alpha(opacity=0);
  -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;
}
<form>
    <div class="stars">
        <input type="radio" name="star_a" class="star-1" id="star_a-1" />
        <label class="star-1" for="star_a-1">1</label>
        <input type="radio" name="star_a" class="star-2" id="star_a-2" />
        <label class="star-2" for="star_a-2">2</label>
        <input type="radio" name="star_a" class="star-3" id="star_a-3" />
        <label class="star-3" for="star_a-3">3</label>
        <input type="radio" name="star_a" class="star-4" id="star_a-4" />
        <label class="star-4" for="star_a-4">4</label>
        <input type="radio" name="star_a" class="star-5" id="star_a-5" />
        <label class="star-5" for="star_a-5">5</label>
        <span></span>
    </div>
    <div class="stars">
        <input type="radio" name="star_b" class="star-1" id="star_b-1" />
        <label class="star-1" for="star_b-1">1</label>
        <input type="radio" name="star_b" class="star-2" id="star_b-2" />
        <label class="star-2" for="star_b-2">2</label>
        <input type="radio" name="star_b" class="star-3" id="star_b-3" />
        <label class="star-3" for="star_b-3">3</label>
        <input type="radio" name="star_b" class="star-4" id="star_b-4" />
        <label class="star-4" for="star_b-4">4</label>
        <input type="radio" name="star_b" class="star-5" id="star_b-5" />
        <label class="star-5" for="star_b-5">5</label>
        <span></span>
    </div>
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    相关资源
    最近更新 更多