两个星级评级系统总是输出第二个父级ID的ID

时间:2018-03-19 18:39:28

标签: javascript jquery html css

问题是我有两个星级评分系统。每个星级评分系统的父级fieldset都有不同的ID。如果用户点击第一个星级评分系统,我希望它输出该父容器的ID,如果他们点击第二个星级评分系统,我希望它输出该父容器的ID。

但是,两个星级评分系统都会输出第二个(最后一个)字段集容器的ID。

$(document).on('change', 'input[type="radio"]', function (e) {
        console.log($(this).parent().attr('id'));
});
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" id="93">
  <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" id="23">
  <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>

如果您查看我的问题Onclick event runs twice,您会看到答案是:

  • 为每个输入提供一个唯一的ID(因为它们是两组星级评定系统)
  • 更改HTML结构,以便每个输入都在标签内。

不幸的是,这些都没有真正帮助,因为第一个选项不方便更改每个星级评级集的ID,而第二个选项,它会破坏我的CSS,如Stars (radio button) won't change colour when selected

我该怎么办?

1 个答案:

答案 0 :(得分:0)

问题确实是重复ID ,因为您不想手动(可理解),可能因为有数百种或其他原因。以下方法是对已生成的标记进行后处理(如果您自己生成标记,请按照生成它们的相同技术,而不是AFTER)。

像这样,每个input都有自己唯一的ID和(非常重要!)相应的label也会有必要的for属性。

随意重构效率。

&#13;
&#13;
$(document).ready(function(){
$('fieldset').each(function(){
          var parentId = $(this).attr('id');
          $(this).children('input').each(function(){
            var newId = $(this).attr('id') + "-" + parentId;
            $(this).attr('id', $(this).attr('id') + "-" + parentId);
            $(this).next('label').attr('for', newId);
        });
        });
});

$(document).on('change', 'input[type="radio"]', function (e) {
        console.log($(this).parent().attr('id'));
});
&#13;
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;
}
&#13;
<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" id="93">
  <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" id="23">
  <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>
&#13;
&#13;
&#13;