字段集onclick获取单选按钮单击值

时间:2016-12-21 20:37:05

标签: javascript jquery asp.net onclick fieldset

我的HTML中有一个字段集,基本上代表了以下五星评级系统:

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

这些类中的任何一个都可以成为问题的根源吗?

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

您可以将其他事件附加到其他地方的checkbox,但您可以根据您的OP代码查看下面的工作代码段,显示console.log只需点击一次即可点击。

我建议直接将点击附加到输入,只需使用:

获取当前检查的值
$(this).val();

希望这有帮助。

&#13;
&#13;
$('#ratingSystem input').click(function () {
  console.log($(this).val());
});
&#13;
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;  } 
&#13;
<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>
&#13;
&#13;
&#13;