radioボタンの選択によって、selectのoptionを表示非表示にしたかったですが、
IE、iOSではうまくいきませんでした。
<input type="radio" name="sample" id="radio01" onclick="entryChange1();" value="エー"/><label for="radio01">エー</label> <input type="radio" name="sample" id="radio02" onclick="entryChange1();" value="ビー" /><label for="radio02">ビー</label>
ラジオボタンの「ビー」が選択されたときのみセレクトメニュー「20:00~21:00」を非表示にする
<select name="セレクトサンプル" id="seminar-time"> <option value="" selected="selected">お選び下さい</option> <option value="11:00~12:00">11:00~12:00</option> <option value="14:00~15:00">14:00~15:00</option> <option value="20:00~21:00" id="none-radio02">20:00~21:00</option> </select>
function entryChange1(){ radio = document.getElementsByName('sample') if(radio[0].checked) { //A.1つ目が選択されたら下記を実行 document.getElementById('seminar-time').disabled = false; document.getElementById('none-radio02').hidden = false; var obj = document.getElementById('seminar-time'); obj.selectedIndex = 0; }else if(radio[1].checked) { //B.2つ目が選択されたら下記を実行 document.getElementById('seminar-time').disabled = false; document.getElementById('none-radio02').hidden = true; var obj = document.getElementById('seminar-time'); obj.selectedIndex = 0; } } window.onload = entryChange1;
こんな感じで、chrome、firefoxはうまく動作しました。
解決方法を調べていて、
下記URLの解決策もやってみたのですが、optionをspanの入れ子にしたら、切り替えたいoptionがラジオボタンの選択を切り替えても非表示のままでうまくいきませんでした。
https://netamame.com/safari-select-option_display-none
ご教示願います。
追記です。
function entryChange1(){ radio = document.getElementsByName('sample') if(radio[0].checked) { document.getElementById('seminar-time').disabled = false; $('#none-radio02').unwrap("<span>"); var obj = document.getElementById('seminar-time'); obj.selectedIndex = 0; }else if(radio[1].checked) { document.getElementById('seminar-time').disabled = false; $('#none-radio02').wrap("<span>"); var obj = document.getElementById('seminar-time'); obj.selectedIndex = 0; } } window.onload = entryChange1;
上記のようにすると
ラジオボタン「エー」を2回押してしまうと
$('#none-radio02').unwrap("<span>");
があるため<select>タグが消えてしまいます。
<option value="" selected="selected">お選び下さい</option> <option value="11:00~12:00">11:00~12:00</option> <option value="14:00~15:00">14:00~15:00</option> <option value="20:00~21:00" id="none-radio02">20:00~21:00</option>
いい方法はありませんでしょうか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/28 05:13
2019/05/28 05:16
2019/05/28 05:34
2019/05/28 05:51
2019/05/28 06:11
2019/05/28 06:14
2019/05/28 06:27
2019/05/28 06:40
2019/05/28 06:54
2019/05/28 08:23
2019/05/28 08:32
2019/05/28 08:39