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

Fieldset onclick getting radio button clicked value

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

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

您可以将另一个事件附加到 checkbox 的其他地方,但您可以根据您的 OP 代码检查下面的工作代码段显示 console.log 仅在点击时触发一次。

我建议将点击直接附加到输入上,只需使用即可获取当前选中的值:

$(this).val();

希望对您有所帮助。

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

尝试将您的活动附加到每个 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>