gpt4 book ai didi

javascript - Fieldset onclick 获取单选按钮的点击值

转载 作者:行者123 更新时间:2023-11-30 08:28:36 25 4
gpt4 key购买 nike

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

这些类中的任何一个可能是问题的根源吗?

最佳答案

尝试将事件附加到每个 input 元素:

$('#ratingSystem input').click(function() {
console.log($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<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>

关于javascript - Fieldset onclick 获取单选按钮的点击值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41271498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com