在表单输入前得到 div 和特定的 class
get div with specific class before form input
我有:
<div class="col-md-3 col-centered survey-kind-border">
<a class="lv-item" href="#">
<div class="media">
<label for="no" class="pull-left">
<div class="survey_kind_choice">
1.5
</div>
<input checked="checked" class="survey_kind_input" id="no" type="radio" value="no">
</label>
<div class="media-body">
<div class="lv-title">
No
</div>
<small class="lv-small">
I will Not
</small>
</div>
</div>
</a>
</div>
<div class="col-md-3 col-centered ">
<a class="lv-item" href="#">
<div class="media">
<label for="yes" class="pull-left">
<div class="survey_kind_choice">
2.5
</div>
<input class="survey_kind_input" id="yes" type="radio" value="yes">
</label>
<div class="media-body">
<div class="lv-title">
Yes
</div>
<small class="lv-small">
I will
</small>
</div>
</div>
</a>
</div>
当有人访问此页面时,通常已经选择了其中一个单选按钮。如果选择了该按钮,我想获取与该表单元素关联且具有 class "survey_kind_choice" 的 div 并向其添加 class。我正在尝试使用以下代码来做到这一点:
if($('.survey_kind_input').is(':checked')){
alert('there is a checked element');
$(this).prev('.survey_kind_choice').addClass('current_choice');
}
警报有效,但我无法添加 class。我做错了什么,我该如何解决?
不需要 if 语句,只需在选中的输入上执行选择器:
$('.survey_kind_input:checked').prev('.survey_kind_choice').addClass('current_choice');
我有:
<div class="col-md-3 col-centered survey-kind-border">
<a class="lv-item" href="#">
<div class="media">
<label for="no" class="pull-left">
<div class="survey_kind_choice">
1.5
</div>
<input checked="checked" class="survey_kind_input" id="no" type="radio" value="no">
</label>
<div class="media-body">
<div class="lv-title">
No
</div>
<small class="lv-small">
I will Not
</small>
</div>
</div>
</a>
</div>
<div class="col-md-3 col-centered ">
<a class="lv-item" href="#">
<div class="media">
<label for="yes" class="pull-left">
<div class="survey_kind_choice">
2.5
</div>
<input class="survey_kind_input" id="yes" type="radio" value="yes">
</label>
<div class="media-body">
<div class="lv-title">
Yes
</div>
<small class="lv-small">
I will
</small>
</div>
</div>
</a>
</div>
当有人访问此页面时,通常已经选择了其中一个单选按钮。如果选择了该按钮,我想获取与该表单元素关联且具有 class "survey_kind_choice" 的 div 并向其添加 class。我正在尝试使用以下代码来做到这一点:
if($('.survey_kind_input').is(':checked')){
alert('there is a checked element');
$(this).prev('.survey_kind_choice').addClass('current_choice');
}
警报有效,但我无法添加 class。我做错了什么,我该如何解决?
不需要 if 语句,只需在选中的输入上执行选择器:
$('.survey_kind_input:checked').prev('.survey_kind_choice').addClass('current_choice');