2016-08-31 23 views
1

私はラジオボタンをスタイリングして商品を注文するためにダイナミックフォームを構築しています。私はすべてのCSSを追加していませんが、私が持っている問題を見るのに十分です。ラジオボタンが選択されているときにクラスを追加するためのjavascriptがありますが、正しく動作していません。グループ1のオプションの1つを選択すると、ボタンが緑色に変わり、緑色のままになります。グループ2のオプションを選択すると、そのボタンも緑色に変わります。グループの背景色のラベルを選択

いくつかのオプション(ラジオグループ)を追加し、各グループに緑色のラジオボタンが選択されているようにJavaScriptを更新する必要がありますが、javascriptについてはあまりよく分かりません。

ラベルごとに多少のタイプの配列がありますか?代わりに、スクリプト内のすべてのラベルを取得し、btn-primary3 classsを除去する

$('label').click(function() { 
 
    $('label').removeClass('btn-primary3'); 
 
    $(this).addClass('btn-primary3'); 
 
}); 
 

 
$('input:checked').parent().addClass('btn-primary3');
li.button-list {width:100%; margin:0px 0px 35px -20px; list-style:none; font-size:16px !important; position:relative; text-align:center;} 
 
.checked{display:none;} 
 
input.noradio {visibility: hidden;margin-left:-10px; } 
 
label.btn-primary2 {background-color:#333; border-color:#333;font-size:16px !important; color:#fff; padding:10px;} 
 
label.btn-primary3 {background-color:#008902 !important; border-color:#008902 !important;font-size:16px !important;} 
 
label.btn-primary2:hover {background-color:#008902 !important; border-color:#008902 !important;} 
 
label.btn-primary2:focus {background-color:#008902 !important; border-color:#008902 !important;} 
 
label.btn-primary3:hover {background-color:#008902 !important; border-color:#008902 !important;} 
 
label.btn-primary3:focus {background-color:#008902 !important; border-color:#008902 !important;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<h3 style="text-align:center;">First Radio Group</h3> 
 
<ul class="btn-group" id="input_14_20"> 
 

 
<li class="gchoice_14_20_0 button-list" > 
 
<label class="wbc-button button btn btn-primary btn-primary2 active" for="choice_14_20_0" id="label_14_20_0"> 
 
<input type="radio" name="input_20" class="noradio" id="choice_14_20_0" tabindex="1" onclick="gf_apply_rules(14,[0,92,99]);" onkeypress="gf_apply_rules(14,[0,92,99]);" autocomplete="off" checked="checked" value="Standard Floor Plan (King-Size Bed)|49900"> 
 
<i class="fa fa-check checked" style="margin-left:-15px;"></i> <i class="fa fa-times unchecked" style="margin-left:-15px;"></i> Option 1</label> 
 
</li> 
 

 

 
<li class="gchoice_14_20_1 button-list"> 
 
<label class="wbc-button button btn btn-primary btn-primary2"> 
 
<input type="radio" name="input_20" class="noradio" id="choice_14_20_1" tabindex="2" value="Twin Bed Floor Plan|49900" onclick="gf_apply_rules(14,[0,92,99]);" onkeypress="gf_apply_rules(14,[0,92,99]);" autocomplete="off"><i class="fa fa-check checked" style="margin-left:-15px;"></i> <i class="fa fa-times unchecked" style="margin-left:-15px;"></i> Option 2</label> 
 
</li> 
 

 
</ul> 
 
    
 
    <h3 style="text-align:center;">Second Radio Group</h3> 
 
    
 
<ul class="btn-group2" id="input_14_24"> 
 

 
<li class="gchoice_14_24_0 button-list"> 
 
<label name="countertops" class="wbc-button button btn btn-primary btn-primary2 active" for="choice_14_24_0" id="label_14_24_0"> 
 
<input type="radio" name="input_24" class="noradio" id="choice_14_24_0" tabindex="3" autocomplete="off" onclick="changeImage(0) gf_apply_rules(14,[0,101]);" onkeypress="gf_apply_rules(14,[0,101]);" checked="checked" value="Standard Countertops|0"> 
 
<i class="fa fa-check checked" style="margin-left:-15px;"></i> <i class="fa fa-times unchecked" style="margin-left:-15px;"></i> Option 1</label> 
 
</li> 
 

 
<li class="gchoice_14_24_1 button-list"> 
 
<label name="countertops" class="wbc-button button btn btn-primary btn-primary2" for="choice_14_24_1" id="label_14_24_1"> 
 
<input type="radio" name="input_24" class="noradio" id="choice_14_24_1" tabindex="4" onclick="changeImage(1) gf_apply_rules(14,[0,101]);" onkeypress="gf_apply_rules(14,[0,101]);" value="Fiber-Granite Countertops|1800" autocomplete="off"><i class="fa fa-check checked" style="margin-left:-15px;"></i> <i class="fa fa-times unchecked" style="margin-left:-15px;"></i> Option 2</label> 
 
</li> 
 

 
</ul>

答えて

1

、あなたがクリックされたものと同じ名前のラップラジオボタンのみのラベルを取得する必要があります。

クリックしたラベル内の入力の名前を取得するには、クリックされたラベルのタイプ "input"の最初の(この場合は唯一の)子を選択し、その名前をa一時変数:

次のステップは、その名前に一致するすべての入力要素の親を選択することです。これらは、関連するラベルも、その後のみからクラスを削除します:

$("input[name='" + inputName + "']").parent().removeClass('btn-primary3'); 

既存のスクリプトにこれを入れて、あなたが希望する結果を得る:

$('label').click(function() { 
 
var inputName = $(this).children("input")[0].name; 
 
$("input[name='" + inputName + "']").parent().removeClass('btn-primary3'); 
 
$(this).addClass('btn-primary3'); 
 
}); 
 

 
$('input:checked').parent().addClass('btn-primary3'); 
 

 
function gf_apply_rules() { } 
 
function changeImage() { }
li.button-list {width:100%; margin:0px 0px 35px -20px; list-style:none; font-size:16px !important; position:relative; text-align:center;} 
 
.checked{display:none;} 
 
input.noradio {visibility: hidden;margin-left:-10px; } 
 
label.btn-primary2 {background-color:#333; border-color:#333;font-size:16px !important; color:#fff; padding:10px;} 
 
label.btn-primary3 {background-color:#008902 !important; border-color:#008902 !important;font-size:16px !important;} 
 
label.btn-primary2:hover {background-color:#008902 !important; border-color:#008902 !important;} 
 
label.btn-primary2:focus {background-color:#008902 !important; border-color:#008902 !important;} 
 
label.btn-primary3:hover {background-color:#008902 !important; border-color:#008902 !important;} 
 
label.btn-primary3:focus {background-color:#008902 !important; border-color:#008902 !important;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<h3 style="text-align:center;">First Radio Group</h3> 
 
<ul class="btn-group" id="input_14_20"> 
 

 
<li class="gchoice_14_20_0 button-list" > 
 
<label class="wbc-button button btn btn-primary btn-primary2 active" for="choice_14_20_0" id="label_14_20_0"> 
 
<input type="radio" name="input_20" class="noradio" id="choice_14_20_0" tabindex="1" onclick="gf_apply_rules(14,[0,92,99]);" onkeypress="gf_apply_rules(14,[0,92,99]);" autocomplete="off" checked="checked" value="Standard Floor Plan (King-Size Bed)|49900"> 
 
<i class="fa fa-check checked" style="margin-left:-15px;"></i> <i class="fa fa-times unchecked" style="margin-left:-15px;"></i> Option 1</label> 
 
</li> 
 

 

 
<li class="gchoice_14_20_1 button-list"> 
 
<label class="wbc-button button btn btn-primary btn-primary2"> 
 
<input type="radio" name="input_20" class="noradio" id="choice_14_20_1" tabindex="2" value="Twin Bed Floor Plan|49900" onclick="gf_apply_rules(14,[0,92,99]);" onkeypress="gf_apply_rules(14,[0,92,99]);" autocomplete="off"><i class="fa fa-check checked" style="margin-left:-15px;"></i> <i class="fa fa-times unchecked" style="margin-left:-15px;"></i> Option 2</label> 
 
</li> 
 

 
</ul> 
 
    
 
    <h3 style="text-align:center;">Second Radio Group</h3> 
 
    
 
<ul class="btn-group2" id="input_14_24"> 
 

 
<li class="gchoice_14_24_0 button-list"> 
 
<label name="countertops" class="wbc-button button btn btn-primary btn-primary2 active" for="choice_14_24_0" id="label_14_24_0"> 
 
<input type="radio" name="input_24" class="noradio" id="choice_14_24_0" tabindex="3" autocomplete="off" onclick="changeImage(0); gf_apply_rules(14,[0,101]);" onkeypress="gf_apply_rules(14,[0,101]);" checked="checked" value="Standard Countertops|0"> 
 
<i class="fa fa-check checked" style="margin-left:-15px;"></i> <i class="fa fa-times unchecked" style="margin-left:-15px;"></i> Option 1</label> 
 
</li> 
 

 
<li class="gchoice_14_24_1 button-list"> 
 
<label name="countertops" class="wbc-button button btn btn-primary btn-primary2" for="choice_14_24_1" id="label_14_24_1"> 
 
<input type="radio" name="input_24" class="noradio" id="choice_14_24_1" tabindex="4" onclick="changeImage(1); gf_apply_rules(14,[0,101]);" onkeypress="gf_apply_rules(14,[0,101]);" value="Fiber-Granite Countertops|1800" autocomplete="off"><i class="fa fa-check checked" style="margin-left:-15px;"></i> <i class="fa fa-times unchecked" style="margin-left:-15px;"></i> Option 2</label> 
 
</li> 
 

 
</ul>

注意を「gf_apply_rules」と「changeImage」のプレースホルダ関数を追加し、onclickハンドラのセミコロン構文エラーが修正され、スニペットのエラーを回避しました。

+0

これは意味があります。私はゆっくりとjavascriptの仕組みに傾いています。それを説明していただきありがとうございます! –

関連する問題