Onclick事件运行两次

时间:2018-03-18 18:56:12

标签: javascript jquery html css

我似乎无法理解为什么当用户点击星标时我的onclick()事件会触发两次。

如果用户点击第一组中的第一个星标,它应该将10.5输出到控制台,而是输出1undefined 10.5

第一个值应该表示隐藏输入字段的值,第二个值应该表示无线电/星的值。

$(document).on('click', 'fieldset', function () {
    console.log($(this).find("input[type='hidden']").val());
    console.log($(this).find("input[type='radio']:checked").val());
});
fieldset,
label {
  margin: 0;
  padding: 0;
  margin-bottom: 20px;
}

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

.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>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"/>

<fieldset class="rating">
<input type="hidden" value="1">
  <input type="radio" id="5star" name="rating" value="5" />
  <label class="full" for="5star" title="Excellent"></label>

  <input type="radio" id="4halfstar" name="rating" value="4.5" />
  <label class="half" for="4halfstar" title="Good"></label>

  <input type="radio" id="4star" name="rating" value="4" />
  <label class="full" for="4star" title="Pretty good"></label>

  <input type="radio" id="3halfstar" name="rating" value="3.5" />
  <label class="half" for="3halfstar" title="Nice"></label>

  <input type="radio" id="3star" name="rating" value="3" />
  <label class="full" for="3star" title="Ok"></label>

  <input type="radio" id="2halfstar" name="rating" value="2.5" />
  <label class="half" for="2halfstar" title="Kinda bad"></label>

  <input type="radio" id="2star" name="rating" value="2" />
  <label class="full" for="2star" title="Bad"></label>

  <input type="radio" id="1halfstar" name="rating" value="1.5" />
  <label class="half" for="1halfstar" title="Meh"></label>

  <input type="radio" id="1star" name="rating" value="1" />
  <label class="full" for="1star" title="Umm"></label>

  <input type="radio" id="halfstar" name="rating" value="0.5" />
  <label class="half" for="halfstar" title="Worst"></label>

</fieldset>

<br><br>

<fieldset class="rating">
<input type="hidden" value="2">
  <input type="radio" id="5star" name="rating" value="5" />
  <label class="full" for="5star" title="Excellent"></label>

  <input type="radio" id="4halfstar" name="rating" value="4.5" />
  <label class="half" for="4halfstar" title="Good"></label>

  <input type="radio" id="4star" name="rating" value="4" />
  <label class="full" for="4star" title="Pretty good"></label>

  <input type="radio" id="3halfstar" name="rating" value="3.5" />
  <label class="half" for="3halfstar" title="Nice"></label>

  <input type="radio" id="3star" name="rating" value="3" />
  <label class="full" for="3star" title="Ok"></label>

  <input type="radio" id="2halfstar" name="rating" value="2.5" />
  <label class="half" for="2halfstar" title="Kinda bad"></label>

  <input type="radio" id="2star" name="rating" value="2" />
  <label class="full" for="2star" title="Bad"></label>

  <input type="radio" id="1halfstar" name="rating" value="1.5" />
  <label class="half" for="1halfstar" title="Meh"></label>

  <input type="radio" id="1star" name="rating" value="1" />
  <label class="full" for="1star" title="Umm"></label>

  <input type="radio" id="halfstar" name="rating" value="0.5" />
  <label class="half" for="halfstar" title="Worst"></label>

</fieldset>

1 个答案:

答案 0 :(得分:3)

代码问题在于点击label fieldset上的click input事件。所以你真的有两次点击 - 首先是标签,第二次是相关的input radio

因此,您需要做的是跟踪change的{​​{1}}事件,而不是跟踪radio上的点击次数。

更新:正如Temani Afif在评论中提到的那样,因为输入有非唯一 ID,所以在第二个{{1}点击fieldset仍然会在第一个 radio中获得fieldset的值。因此,您需要替换标签ID

更多input[type='hidden']的一个更好的做法是将fieldset包含在其中,因此您的标记类似于:

label

在这种情况下,您不需要input<label class="half" title="Good"> <input type="radio" name="rating" value="4.5" /> </label> ,因为id适用于其中的元素。

&#13;
&#13;
for
&#13;
label
&#13;
$(document).on('change', 'input[type="radio"]', function (e) {
    console.log(
        $(this).val(), 
        $(this).parent().find("input[type='hidden']").val()
    );
});
&#13;
&#13;
&#13;