【发布时间】:2016-12-21 20:37:04
【问题描述】:
我的 HTML 中有一个字段集,它基本上代表 5 星评级系统,如下所示:
<fieldset class="rating" id="ratingSystem">
<input type="radio" id="star5" name="rating" value="5" /><label class="full" for="star5" title="Awesome - 5 stars"></label>
<input type="radio" id="star4" name="rating" value="4" /><label class="full" for="star4" title="Pretty good - 4 stars"></label>
<input type="radio" id="star3" name="rating" value="3" /><label class="full" for="star3" title="Meh - 3 stars"></label>
<input type="radio" id="star2" name="rating" value="2" /><label class="full" for="star2" title="Kinda bad - 2 stars"></label>
<input type="radio" id="star1" name="rating" value="1" /><label class="full" for="star1" title="Sucks big time - 1 star"></label>
</fieldset>
我已经定义了一个 onclick 事件,它定义了用户何时单击某些星级评分(单选按钮)然后我只是出于测试目的显示单击的值,但我总是显示双精度值而不是显示一个……
例如,如果我点击 5,我会显示在控制台中:
undefined
5
我第二次点击 4 我得到了值:
5
4
我使用的代码是:
$('#ratingSystem').click(function () {
console.log($('input[name=rating]:checked').val());
});
我在这里做错了什么?
编辑,这些是我用来将单选按钮变成星星的 CSS 类,使其看起来像星级评分系统:
fieldset, label { margin: 0; padding: 0; }
body{ margin: 20px; }
h1 { font-size: 1.5em; margin: 10px; }
/****** Style Star Rating Widget *****/
.rating {
border: none;
float: left;
}
.rating > input { display: none; }
.rating > label:before {
margin: 5px;
font-size: 1.25em;
font-family: FontAwesome;
display: inline-block;
content: "\f005";
}
.rating > .half:before {
content: "\f089";
position: absolute;
}
.rating > label {
color: #ddd;
float: right;
}
/***** CSS Magic to Highlight Stars on Hover *****/
.rating > input:checked ~ label, /* show gold star when clicked */
.rating:not(:checked) > label:hover, /* hover current star */
.rating:not(:checked) > label:hover ~ label { color: #FFD700; } /* hover previous stars in list */
.rating > input:checked + label:hover, /* hover current star when changing rating */
.rating > input:checked ~ label:hover,
.rating > label:hover ~ input:checked ~ label, /* lighten current selection */
.rating > input:checked ~ label:hover ~ label { color: #FFED85; }
这些类中的任何一个都可能是问题的根源吗?
【问题讨论】:
-
当我运行 jsfiddle 中提供的代码时,您能提供更多信息吗?我没有遇到任何问题。 jsfiddle.net/fq56bxzz
-
你确定没有另一组
input[name=rating]在页面上的其他地方,可能是隐藏的吗? -
@User987 完美,这可能是 css 是一些阻塞元素。我自己也遇到过几次这个问题。
-
@User987 我添加了css,现在我遇到了问题。一会儿我会帮你解答的:)
-
我认为伪元素在乱搞
标签: javascript jquery asp.net onclick fieldset