JavaScriptでフォームのテキストボックスの入力無効化、有効化を切り替える。
<input type="text" value="test" disabled />
<select id="ex_age"> <option value="under">20歳未満</option> <option value="over">20歳以上</option> </select> <input id="ex_box" type="text" disabled /> <script> document.getElementById("ex_age").addEventListener("change", function(){ var age_elem = document.getElementById("ex_age"); var s_value = age_elem.options[age_elem.selectedIndex].value; var box_elem = document.getElementById("ex_box"); if(s_value == "under"){ box_elem.disabled = true; }else{ box_elem.disabled = false; } }, false); </script>
<select id="ex_age_a"> <option value="under">20歳未満</option> <option value="over">20歳以上</option> </select> <div id="ex_box_div_a" style="display:none;"> <input id="ex_box_a" type="text" /> </div> <script> document.getElementById("ex_age_a").addEventListener("change", function(){ var age_elem = document.getElementById("ex_age_a"); var s_value = age_elem.options[age_elem.selectedIndex].value; var box_div_elem = document.getElementById("ex_box_div_a"); if(s_value == "under"){ box_div_elem.style.display = 'none'; }else{ box_div_elem.style.display = 'block'; } }, false); </script>
<select id="ex_age_b"> <option value="under">20歳未満</option> <option value="over">20歳以上</option> </select> <div id="ex_box_div_b" style="display:none;"> <input id="ex_box_b" type="text" /> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> document.getElementById("ex_age_b").addEventListener("change", function(){ var age_elem = document.getElementById("ex_age_b"); var s_value = age_elem.options[age_elem.selectedIndex].value; if(s_value == "under"){ $("#ex_box_div_b").hide("normal"); }else{ $("#ex_box_div_b").show("normal"); } }, false); </script>