如何获得两个CSS评级星形小部件,以便在单击时不会相互影响

时间:2015-01-30 18:21:53

标签: html css

谢谢你看看......

请参阅以下fiddle,其中显示了两个仅限CSS的星级小部件或下面的代码。当您在第二个小部件上选择评级时,由于某种原因,它总是会更改第一个小部件上的评级。知道我的CSS中的错误在哪里?对不起,如果这是一个基本问题,我对CSS还不是很好。谢谢!

HTML:

<div class="stars">
  <div class="rating" style="width:65%"></div>
  <input type="radio" name="rating" id="star5" value="5">
  <label for="star5"></label>
  <input type="radio" name="rating" id="star4" value="4">
  <label for="star4"></label>
  <input type="radio" name="rating" id="star3" value="3">
  <label for="star3"></label>
  <input type="radio" name="rating" id="star2" value="2">
  <label for="star2"></label>
  <input type="radio" name="rating" id="star1" value="1">
  <label for="star1"></label>
</div>
</br>
<div class="stars">
  <div class="rating" style="width:65%"></div>
  <input type="radio" name="rating" id="star5" value="5">
  <label for="star5"></label>
  <input type="radio" name="rating" id="star4" value="4">
  <label for="star4"></label>
  <input type="radio" name="rating" id="star3" value="3">
  <label for="star3"></label>
  <input type="radio" name="rating" id="star2" value="2">
  <label for="star2"></label>
  <input type="radio" name="rating" id="star1" value="1">
  <label for="star1"></label>
</div>

CSS:

.stars{
    width: 130px;
    height: 26px;
    background: url(http://sandbox.bumbu.ru/ui/external/stars.png) 0 0 repeat-x;
    position: relative;
}

.stars .rating{
    height: 26px;
    background: url(http://sandbox.bumbu.ru/ui/external/stars.png) 0 -26px repeat-x;
}

.stars input{
    display: none;
}

.stars label{
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    height: 26px;
    width: 130px;
    cursor: pointer;
}
.stars:hover label{
    display: block;
}
.stars label:hover{
    background: url(http://sandbox.bumbu.ru/ui/external/stars.png) 0 -52px repeat-x;
}

.stars label + input + label{width: 104px;}
.stars label + input + label + input + label{width: 78px;}
.stars label + input + label + input + label + input + label{width: 52px;}
.stars label + input + label + input + label + input + label + input + label{width: 26px;}

.stars input:checked + label{
    display: block;
    background: url(http://sandbox.bumbu.ru/ui/external/stars.png) 0 -52px repeat-x;
}

1 个答案:

答案 0 :(得分:3)

HTML中的

Id's are unique。您不能在页面上拥有多个具有相同ID的元素,并期望它的行为。您可以看到这种行为。由于您的标签已将for属性设置为id,因此浏览器会检查输入的第一个ID,因为它只是期待一个

每个广播组还需要具有唯一的name属性,否则所有广播都将被视为单个广告组,并且只能在页面上选择一个广告。

因此您需要更改第二组无线电的ID:

<div class="stars">
  <div class="rating" style="width:65%"></div>
  <input type="radio" name="rating" id="star5" value="5">
  <label for="star5"></label>
  <input type="radio" name="rating" id="star4" value="4">
  <label for="star4"></label>
  <input type="radio" name="rating" id="star3" value="3">
  <label for="star3"></label>
  <input type="radio" name="rating" id="star2" value="2">
  <label for="star2"></label>
  <input type="radio" name="rating" id="star1" value="1">
  <label for="star1"></label>
</div>
</br>
<div class="stars">
  <div class="rating" style="width:65%"></div>
  <input type="radio" name="ratinga" id="star5a" value="5">
  <label for="star5a"></label>
  <input type="radio" name="ratinga" id="star4a" value="4">
  <label for="star4a"></label>
  <input type="radio" name="ratinga" id="star3a" value="3">
  <label for="star3a"></label>
  <input type="radio" name="ratinga" id="star2a" value="2">
  <label for="star2a"></label>
  <input type="radio" name="ratinga" id="star1a" value="1">
  <label for="star1a"></label>
</div>