<p><label class="label01"><input type="radio" name="radio1" id="radio" value="radio1" />1</label>
<label class="label02"><input type="radio" name="radio1" id="radio2" value="radio2" />2</label></p>
<p><label class="label03"><input type="radio" name="radio2" id="radio3" value="radio3" />3</label>
<label class="label04"><input type="radio" name="radio2" id="radio3" value="radio4" />4</label></p>
<div class"html1">
</div>
<div class"html2">
</div>
このようなラジオボタンが有り、チェックが入った際に、
・div.html1に予め指定した文言を表示
・div.html2にvalueの値を出力
・クリックされたラベルにCSSで背景色を付ける
javascriptでどのように記述すればよいでしょうか。
radio1の時は あいうえお を表示
radio2の時は かきくけこ
radio3の時は さしすせそ
radio4の時は たちつてと
radio1とradio3の時は いろは
radio1とradio4の時は にほへと
radio2とradio3の時は あかさたな
radio2とradio4の時は はひふへほ
ラベルにつけたい色は #ddd
こんな感じ?
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> <script> var html1_text = { "false|false|false|false|" : "", "true|false|false|false|" : "あいうえお", "false|true|false|false|" : "かきくけこ", "false|false|true|false|" : "さしすせそ", "false|false|false|true|" : "たちつてと", "true|false|true|false|" : "いろは", "true|false|false|true|" : "にほへと", "false|true|true|false|" : "あかさたな", "false|true|false|true|" : "はひふへほ", "" : "" }; $(function() { $("input[type=radio]").change(function() { // html1 $(".html1").html(html1_text[ this.id ]); var radio_status = ""; $("input[type=radio]").each(function() { radio_status = radio_status + this.checked + "|"; }); $(".html1").html(html1_text[ radio_status ]); // html2 $(".html2").html($(this).val()); // label color $("input[type=radio]").each(function() { $(this).parent().get(0).style.color = ""; }); $(this).parent().get(0).style.color = "#ddd"; }); }); </script> <p><label class="label01"><input type="radio" name="radio1" id="radio" value="radio1" />1</label> <label class="label02"><input type="radio" name="radio1" id="radio2" value="radio2" />2</label></p> <p><label class="label03"><input type="radio" name="radio2" id="radio3" value="radio3" />3</label> <label class="label04"><input type="radio" name="radio2" id="radio4" value="radio4" />4</label></p> <div class="html1"> </div> <div class="html2"> </div>
jQuery を使ってます。
jsFiddle で試してみたのが、これ。
http://jsfiddle.net/tnXNJ/1/
質問文にあるコードで、radio4 の id が radio3 になってたのと、div の class 属性に = が付いてないのを修正してます。