显示星星而不是单选按钮

时间:2016-07-07 05:56:10

标签: php html css magento

我们为访问者提供了一个选项,如下图所示。他们会选择单选按钮&他们会点击submit按钮。

enter image description here

但我希望更改为this,意味着我们应点击Radio buttons

而不是点击stars

enter image description here

我可以通过以下代码隐藏星星:

.ratings .rating-box .rating 
{
    display : none;
}

现在我想将Radio buttons更改为星标。另外,我想在第二张图像中显示彼此更近的每颗星。

CSS

  input[type="radio"] {
        box-sizing: border-box;
        padding: 0;
    }

.checkbox, .radio {
    position: relative;
    top: -1px;
    display: inline-block;
}

PHTML

<?php if( $this->getRatings() && $this->getRatings()->getSize()): ?>
                <h4><?php echo $this->__('Your Rating') ?> <em class="required"></em></h4>
                <span id="input-message-box"></span>
                <table class="data-table review-summary-table ratings" id="product-review-table">
                    <col width="1" />
                    <col />
                    <col />
                    <col />
                    <col />
                    <col />
                    <thead>
                        <tr>
                            <th>&nbsp;</th>
                            <th>
                                <div class="rating-box">
                                    <span class="rating-number">1</span>
                                    <span class="rating nobr" style="width:20%;"><?php echo $this->__('1 star') ?></span>
                                </div>
                            </th>
                            <th>
                                <div class="rating-box">
                                    <span class="rating-number">2</span>
                                    <span class="rating nobr" style="width:40%;"><?php echo $this->__('2 star') ?></span>
                                </div>
                            </th>
                            <th>
                                <div class="rating-box">
                                    <span class="rating-number">3</span>
                                    <span class="rating nobr" style="width:60%;"><?php echo $this->__('3 star') ?></span>
                                </div>
                            </th>
                            <th>
                                <div class="rating-box">
                                    <span class="rating-number">4</span>
                                    <span class="rating nobr" style="width:80%;"><?php echo $this->__('4 star') ?></span>
                                </div>
                            </th>
                            <th>
                                <div class="rating-box">
                                    <span class="rating-number">5</span>
                                    <span class="rating nobr" style="width:100%;"><?php echo $this->__('5 star') ?></span>
                                </div>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($this->getRatings() as $_rating): ?>
                        <tr>
                            <th><?php echo $this->escapeHtml($_rating->getRatingCode()) ?></th>
                        <?php foreach ($_rating->getOptions() as $_option): ?>
                            <td class="value"><label for="<?php echo $this->escapeHtml($_rating->getRatingCode()) ?>_<?php echo $_option->getValue() ?>"><input type="radio" name="ratings[<?php echo $_rating->getId() ?>]" id="<?php echo $this->escapeHtml($_rating->getRatingCode()) ?>_<?php echo $_option->getValue() ?>" value="<?php echo $_option->getId() ?>" class="radio" /></label></td>
                        <?php endforeach; ?>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
                <input type="hidden" name="validate_rating" class="validate-rating" value="" />
                <script type="text/javascript">decorateTable('product-review-table')</script>
            <?php endif; ?>

您可以访问link并点击“添加评论”标签查看单选按钮和星标

2 个答案:

答案 0 :(得分:4)

这样做

&#13;
&#13;
.wrapper {
  display: inline-block;
}
.wrapper * {
  float: right;
}
input {
  display: none;
}
label {
  font-size: 30px;
}

input:checked ~ label {
  color: red;
}
/*
label:hover,
label:hover ~ label {
  color: red;
}
*/
/*
input:checked ~ label:hover,
input:checked ~ label:hover ~ label {
  color: lime !important;
}
*/
&#13;
<div class="wrapper">
  <input type="radio" id="r1" name="rg1">
  <label for="r1">&#10038;</label>
  <input type="radio" id="r2" name="rg1">
  <label for="r2">&#10038;</label>
  <input type="radio" id="r3" name="rg1">
  <label for="r3">&#10038;</label>
  <input type="radio" id="r4" name="rg1">
  <label for="r4">&#10038;</label>
  <input type="radio" id="r5" name="rg1">
  <label for="r5">&#10038;</label>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这是5 4 3 2 1订单中单选按钮的HTML代码。

<div class="starRating">
  <input id="r5" type="radio" name="rating" value="5">
  <label for="r5">5</label>
  <input id="r4" type="radio" name="rating" value="4">
  <label for="r4">4</label>
  <input id="r3" type="radio" name="rating" value="3">
  <label for="r3">3</label>
  <input id="r2" type="radio" name="rating" value="2">
  <label for="r2">2</label>
  <input id="r1" type="radio" name="rating" value="1">
  <label for="r1">1</label>
</div>

现在添加一些CSS元素来取代星星,然后将顺序反转为1 2 3 4 5。

.starRating:not(old){
  display        : inline-block;
  width          : 7.5em;
  height         : 1.5em;
  overflow       : hidden;
  vertical-align : bottom;
}

现在隐藏单选按钮:

.starRating:not(old) > input{
  margin-right : -100%;
  opacity      : 0;
}

关闭州明星或未评级的明星

.starRating:not(old) > label{
  display         : block;
  float           : right;
  position        : relative;
  background      : url('starimg.png'); // copy your star image url Here
  background-size : contain;
}

标签的伪元素。这个元素向下推并最终隐藏标签。(单选按钮)

.starRating:not(old) > label:before{
  content         : '';
  display         : block;
  width           : 1.5em;
  height          : 1.5em;
  background      : url('starimg.png'); // copy your star image url Here
  background-size : contain;
  opacity         : 0;
  transition      : opacity 0.2s linear;
}

现在,根据上面定义的元素代码确定星星的状态

.starRating:not(old) > label:hover:before,
.starRating:not(old) > label:hover ~ label:before,
.starRating:not(:hover) > :checked ~ label:before{
  opacity : 1;
}

希望这有助于你!