ラジオボタンの選択でテキストフィールドの入力を制御するJavaScriptの正しさについて

このQ&Aのポイント
  • 特定のラジオボタンが選択されたらテキストフィールドを入力可能にするためのJavaScriptを作成しました。
  • 初めてのJavaScriptですが、動作は正常に確認できました。
  • ラジオボタンが選択された時に適切な制御が行われるか、引き続きテストしてください。
回答を見る
  • ベストアンサー

特定のラジオボタンが選択されたらテキストフィールドを入力可

下のような6つのラジオボタンと1つのテキストフィールドのあるフォームがあります。 「あ・い・う・え・お」のラジオボタンが選択されたらテキストフィールドをdisabledに、「か」なら入力可能にしたくて、JavaScriptで次のように記述しました。 一応動作しているように見えるのですが、見よう見まねで書いた初めてJavaScriptなので大丈夫なのか不安です。これで良いのでしょうか。 function txtClose () { if (document.f_frm.motive[5].checked) { document.f_frm.motive_add.disabled = false; } else { document.f_frm.motive_add.disabled = true; } } <form name="f_frm"> <input type="radio" name="motive" value="あ" onclick="txtClose();" />あ<br /> <input type="radio" name="motive" value="い" onclick="txtClose();" />い<br /> <input type="radio" name="motive" value="う" onclick="txtClose();" />う<br /> <input type="radio" name="motive" value="え" onclick="txtClose();" />え<br /> <input type="radio" name="motive" value="お" onclick="txtClose();" />お<br /> <input type="radio" name="motive" value="か" onclick="txtClose();" />か<br /> <input type="text" name="motive_add" size="20" maxlength="40" onclick="txtClose();" /> </form>

質問者が選んだベストアンサー

  • ベストアンサー
  • kazumero
  • ベストアンサー率40% (20/49)
回答No.1

正常動作するので問題ありません。 ただ、テキストフィールドのonclick属性は不必要です。 ラジオボタンが選択された時点で、テキストフィールドの入力可不可が決まりますので。 細かいことを言いますと、 <input type="radio" name="motive" value="あ" onclick="txtClose();" />あ<br /> でなく、 <p><input type="radio" name="motive" value="あ" onclick="txtClose();" />あ</p> のように、<form>内ではbr要素は使用せずにp要素などのブロック要素で囲うのがベストです。

candystore
質問者

お礼

>テキストフィールドのonclick属性は不必要 よく考えると、確かにその通りです。 気付いていませんでした!ありがとうございます。 フォーム内、実際はテーブルの<td>~</td>で囲まれている部分だったのですが、質問の際にその旨を記述していませんでした。すみません。 素早い回答にとても助かりました。ありがとうございます!!

関連するQ&A

  • 複数のラジオボタン選択肢によりsubmitを押せなくする

    初めて質問させていただきます。 ラジオボタンが下記の選択状態の時だけ「送信(submit)」ボタンを enable で表示させたいです。 1:● 2:○ 1:● 2:○ それ以外の選択肢では「送信(submit)」ボタンを disabl で押せないようにしたいです。 調べて、近い動作にはなりましたが、javascriptに関して 殆ど知識がないため、これから先がさっぱりわかりません。 ぜひ教えを乞いたく参りました。お知恵をお貸しください<(__)> どうぞ宜しくお願い致します。 <HTML> <HEAD> <script type="text/javascript"><!-- function unlock1(el){el.form.send.disabled=false;} function unlock2(el){el.form.send.disabled=true;} //--> </script> <form> 1:<input type="radio" name="radio1" value="1" onClick="unlock1(this)"> 2:<input type="radio" name="radio1" value="2" onClick="unlock2(this)"><br> 1:<input type="radio" name="radio2" value="1" onClick="unlock1(this)"><br> 2:<input type="radio" name="radio2" value="2" onClick="unlock2(this)"><br> <input name="send" type="submit" value="送信" disabled > </form> </BODY> </HTML>

  • ラジオボタンが選択によるチェックボックス・ボタンの制御方法

    お世話になります。 やりたい事 (1)ラジオボタン【小学校】:小学校のみ入力可 (2)ラジオボタン【中学校】:中学校(市立・私立)のみ入力可 (3)ラジオボタン【全ての学校】:小中すべて入力可 (1)と(2)は動作していると思います。 (3)を実現するにはどうすればいいのでしょうか? よろしくお願いします。 <html> <head> <title>ラジオボタンが選択されたらチェックボックス・ボタンを入力可能にする</title> <script Language="JavaScript"><!-- function setRTF(f,n){ for(var i=0; i<f.length; i++){ var p=f[i].parentNode; var pid=""; while(p){ if(p.nodeName=="TD"){pid=p.id;break;} p=p.parentNode; } if(f[i].type=="checkbox" || f[i].type=="button") f[i].disabled=pid!=n; } } function allcheck(obj,flag){ var target=obj.parentNode.getElementsByTagName("input"); for(var i=0;i<target.length;i++){ if(target[i].type=="checkbox") target[i].checked=flag; } } // --></script> </head> <body> <form> <table border="1" width="303"> <tr> <td width="89"><input type="radio" name="mm" onClick="setRTF(this.form,'aa')">小学校</td> <td width="90"><input type="radio" name="mm" onClick="setRTF(this.form,'bb')">中学校</td> <td width="102"><input type="radio" name="mm" onClick="setRTF(this.form,'xx')">全ての学校</td> </tr> <tr> <td id="aa" rowspan="2" width="89"> <input type="checkbox" name="aa[]" value="1" disabled>A小学校<br> <input type="checkbox" name="aa[]" value="2" disabled>B小学校<br> <input type="checkbox" name="aa[]" value="3" disabled>C小学校<br> <input type="button" value="全選択" onClick="allcheck(this,true)"/ disabled ><br> <input type="button" value="全解除" onClick="allcheck(this,false)" disabled /> </td> <td id="bb" width="90"> 市立<br> <input type="checkbox" name="bb[]" value="1" disabled>A中学校<br> <input type="checkbox" name="bb[]" value="2" disabled>B中学校<br> <input type="checkbox" name="bb[]" value="3" disabled>C中学校<br> <input type="button" value="全選択" onClick="allcheck(this,true)" disabled /><br> <input type="button" value="全解除" onClick="allcheck(this,false)" disabled /> </td> <td id="cc" rowspan="2" width="102"> </td> </tr> <tr> <td id="bb" width="90"> 私立<br> <input type="checkbox" name="cc[]" value="1" disabled>A中学校<br> <input type="checkbox" name="cc[]" value="2" disabled>B中学校<br> <input type="checkbox" name="cc[]" value="3" disabled>C中学校<br> <input type="button" value="全選択" onClick="allcheck(this,true)" disabled /><br> <input type="button" value="全解除" onClick="allcheck(this,false)" disabled /> </td> </tr> </table> </form> </body> </html>

  • ラジオボタンとテキストを同時にグレーアウトさせる

    <input type=radio name="1" value = 'A1'">A1<br> <input type=radio name="1" value = 'A2'">A2<br> <input type=radio name="1" value = 'A3'">A3<br> <input type=radio name="1" value = 'A4'">A4<br> <input type=radio name="1" value = 'A5'">A5<br> <form> <input type=radio name="tex" onClick="textform.textin.value = 'B1'">B1<br> <input type=radio name="tex" onClick="textform.textin.value = 'B2'">B2<br> <input type=radio name="tex" onClick="textform.textin.value = 'B3'">B3<br> <input type=radio name="tex" onClick="textform.textin.value = 'B4'">B4<br> <input type=radio name="tex" onClick="textform.textin.value = 'B5'">B5<br> </form> <form id="textform" action="#"> ここに文字が表示されます <input id="textin" type="text" size="30"> </form> A1を選択した場合、B2とB4とテキストエリアをグレーアウトさせる A2を選択した場合、B3とテキストエリアをグレーアウトさせる この記述の方法がわかりません。 初心者ですので、どなたかご教授おねがいします。

  • ラジオボタンで入力項目の可・不可を切り替える方法

    雑誌やネットを調べ、ラジオボタンで入力項目の可・不可を切り替える JavaScriptを見つけたのですが、思うように動作しません。 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=shift_jis" /> <title>Sample</title> <script type="text/javascript"> <!-- function radioChange() { if(document.form1.radioSelect[0].checked == true) { document.form1.04text.disabled = false; document.form1.04text.style.backgroundColor='#FFFFFF'; document.form1.05text.disabled = false; document.form1.05text.style.backgroundColor='#FFFFFF'; document.form1.06text.disabled = false; document.form1.06text.style.backgroundColor='#FFFFFF'; document.form1.07text.disabled = true; document.form1.07text.style.backgroundColor='#D4D0C8'; document.form1.08text.disabled = true; document.form1.08text.style.backgroundColor='#D4D0C8'; document.form1.09text.disabled = true; document.form1.09text.style.backgroundColor='#D4D0C8'; } else if(document.form1.radioSelect[1].checked == true) { document.form1.04text.disabled = true; document.form1.04text.style.backgroundColor='#D4D0C8'; document.form1.05text.disabled = true; document.form1.05text.style.backgroundColor='#D4D0C8'; document.form1.06text.disabled = true; document.form1.06text.style.backgroundColor='#D4D0C8'; document.form1.07text.disabled = false; document.form1.07text.style.backgroundColor='#FFFFFF'; document.form1.08text.disabled = false; document.form1.08text.style.backgroundColor='#FFFFFF'; document.form1.09text.disabled = false; document.form1.09text.style.backgroundColor='#FFFFFF'; } else { document.form1.04text.disabled = true; document.form1.04text.style.backgroundColor='#D4D0C8'; document.form1.05text.disabled = true; document.form1.05text.style.backgroundColor='#D4D0C8'; document.form1.06text.disabled = true; document.form1.06text.style.backgroundColor='#D4D0C8'; document.form1.07text.disabled = true; document.form1.07text.style.backgroundColor='#D4D0C8'; document.form1.08text.disabled = true; document.form1.08text.style.backgroundColor='#D4D0C8'; document.form1.09text.disabled = true; document.form1.09text.style.backgroundColor='#D4D0C8'; } } //--> </script> </head> <body onload="radioChange()"> <form action="" method="post" name="form1"> <table> <tr> <td> <input name="radioSelect" type="radio" onclick="radioChange()" value="1" /> 国内旅行 <input name="radioSelect" type="radio" onclick="radioChange()" value="0" /> 海外旅行</td> </tr> <tr> <td>&nbsp;</td> </tr> <tr> <td> 国内編:<br /> 希望地 <input type="text" name="04text" /> <br /> 希望日: <input type="text" name="05text" /> <br /> 乗り物: <input type="text" name="06text" /> </td> </tr> <tr> <td>&nbsp;</td> </tr> <tr> <td>海外編:<br /> 希望地 <input type="text" name="07text" /> <br /> 希望日: <input type="text" name="08text" /> <br /> 乗り物: <input type="text" name="09text" /> </td> </tr> </table> </form> </body> </html> テキストボックスの「name」を、英字から始めれば解決するのですが 事情があり、nameを数字で始める必要があります。 この解決方法を教えていただけませんでしょうか。 よろしくお願いいたします。

  • 選択したたラジオボタンの値をSUBMITボタンに

    選択したたラジオボタンの値をSUBMITボタンの名前として自動的に替えたいのですが、助言をいただけますか。 イメージは下記です。★の部分に、クリックごとにradioの値を入れたいのです。 <script Language="JavaScript"><!-- function set() { n = document.myFORM.Radio.value; } // --> </script> <FORM ACTION="add.cgi" METHOD="POST" name="myFORM"> <INPUT TYPE="RADIO" NAME="Radio" VALUE="東京" onclick="set()" CHECKED> <INPUT TYPE="RADIO" NAME="Radio" VALUE="大阪"> <INPUT TYPE="SUBMIT" NAME="Submit" VALUE="★"></form>

  • ラジオボタンが選択された項目のみ操作可能

    ラジオボタンが選択された項目のみ操作できるようにしたいです。 小学校が選択されたら 小学校のチェックボックスとボタンはenable 中学校のチェックボックスとボタンはdisabled?readonly? よろしくお願いします。 <html> <head> <title>ラジオボタンが選択されたらチェックボックス・ボタンを入力可能にする</title> <script Language="JavaScript"><!-- function setRTF() { fOBJ = document.myFORM; RadioNum = 2; // ラジオボタンの数 for (i=0; i<RadioNum; i++) { if (fOBJ["mm"][i].checked) fOBJ["aa"+i].disabled = false; else fOBJ["bb"+i].disabled = true; } } function allcheck(obj,flag){ var target=obj.parentNode.getElementsByTagName("input"); for(var i=0;i<target.length;i++){ if(target[i].type=="checkbox") target[i].checked=flag; } } // --></script> </head> <body> <form name="myFORM"> <table border="1"> <tr> <td><input type="radio" name="mm" onClick="setRTF()">小学校</td> <td><input type="radio" name="mm" onClick="setRTF()">中学校</td> </tr> <tr> <td> <input type="checkbox" name="aa[]" value="1" />A小学校<br/> <input type="checkbox" name="aa[]" value="2" />B小学校<br/> <input type="checkbox" name="aa[]" value="3" />C小学校<br/> <input type="button" value="全選択" onClick="allcheck(this,true)"/><br> <input type="button" value="全解除" onClick="allcheck(this,false)"/> </td> <td> <input type="checkbox" name="bb[]" value="1" />A中学校<br/> <input type="checkbox" name="bb[]" value="2" />B中学校<br/> <input type="checkbox" name="bb[]" value="3" />C中学校<br/> <input type="button" value="全選択" onClick="allcheck(this,true)"/><br> <input type="button" value="全解除" onClick="allcheck(this,false)"/> </td> </tr> </table> </form> </body> </html>

  • ボタンを押したらラジオボタンを選択したい(サンプルソース付)

    ボタンを押したら、ラジオボタンを選択するようにしたいと思っています。 なんとかチェックボックス、セレクトボックス(?)はうまくいったのですが、どうしてもラジオボタンだけがうまくいきません。どうすればよいでしょうか? ※document.forms[0].R1[0].checked = true; のようにすると意図した通りに動作することはわかったのですが、項目が増えた時にミスの原因になりますので、できるだけ"a"や"b"とvalueを使いたいと思っています。 すみませんが、よろしくお願いします。 -- <html> <head> <SCRIPT language="JavaScript"><!-- function check() { document.forms[0].C1.checked = true; document.forms[0].S1.value = "3"; document.forms[0].R1.value = "a"; } //--></SCRIPT> <title></title> </head> <body> <form> <input type="checkbox" value="yes" name="C1"><br> <select name="S1"> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select><br> <input type="radio" name="R1" value="a"><input type="radio" name="R1" value="b"><br> <input type="button" onclick="javascript:check()"> </form> <br> </body> </html>

  • ラジオボタンの入力制御について(表示した時のみ制御を行う)

    yesの場合はid=ABを表示し、noの場合はid=ABを非表示にする制御を行っています。 両方yesの場合は表示。両方noの場合は非表示。 片方がnullの場合は、yesで表示しnoで非表示としています。 ここまでの動作は、問題ないのですが id=ABを表示した場合、hoge3が入力必須となり id=ABを表示しない場合、hoge3は入力必須ではないように制御を行いたいと思っています。 他書き込みを参考にしたのと、自身で書いたものが混じって非常に汚くなっていますが ご教授願えませんでしょうか。 <script type="text/JavaScript"> <!-- function test1(){ if(document.getElementById("id01").checked) { document.getElementById('AB').style.display = ""; }else if(document.getElementById("id02").checked) { document.getElementById('AB').style.display = ""; for(i = 0; i <= 1; i ++){ document.form1.hoge3[i].checked = false; } }else{ document.getElementById('AB').style.display = "none"; for(i = 0; i <= 1; i ++){ document.form1.hoge3[i].checked = false; } } } //以下はhttp://oshiete1.goo.ne.jp/qa2119460.htmlを参考にしました function test2(f){ for (var i=0;i<f.length;i++){ if(f[i].type=="radio" && f[i].name=="hoge3" && f[i].checked==true) return true; } alert("AかBを選んでください"); return false; } //--> </script> <form name="form1" onload="test1()"onsubmit="return test2(this)"> <br> <input type="radio" name="hoge1" value="1" id="id01" onClick="test1();">yes<br> <input type="radio" name="hoge1" value="0" onClick="test1();">no<br> <br> <input type="radio" name="hoge2" value="1" id="id02" onClick="test1();">yes<br> <input type="radio" name="hoge2" value="0" onClick="test1();">no<br> <br> <p id="AB" style="display:none;"> <input type="radio" name="hoge3" value="1">Aへ進む<br> <input type="radio" name="hoge3" value="2">Bへ進む<br> </p> <br> <input type="submit" value="次へ"> </form>

  • 複数のラジオボタンのチェック

    複数のラジオボタンが全て選択されているかどうかのチェックのJavaScriptをすっきりした形で書くのは、どうしたらいいでしょうか? 下記のようですと、ダラダラと記述するようになってしまいます。 どなたかご教授ください。 --------------------------------------- <script type="text/javascript"> function insert_onClick() { j=0; for (i=0 ;i < document.F1.a11.length ; i++){ if (document.F1.a11[i].checked) { j=1; } } } </script> <form action="check.php" name="F1" onsubmit="return insert_onClick()"> 各valueは0~4まであります <input type="radio" name="a11" value="0"> <input type="radio" name="a12" value="0"> <input type="radio" name="a21" value="0"> <input type="radio" name="a22" value="0"> <input type="radio" name="b11" value="0"> <input type="radio" name="b12" value="0"> <input type="radio" name="b21" value="0"> <input type="radio" name="b22" value="0"> ---------------------------------------

  • (続)ラジオボタンの選択でチェックボックスのON/OFFを連動

    前回(http://okwave.jp/qa5476132.html)とは全く違うものを参考に作成してみましたが、「ラジオボタンを選択するたびにチェックボックスのチェックが初期化される」というのがまだ出来ておりません。 どなたか教えていただけるとありがたいです>< <html> <head> <script language="javascript"> function classDisable(f,cn,fl){ for (var i=0;i<f.elements.length;i++){ if(f.elements[i].className==cn){f.elements[i].disabled=fl} } } function classCheck(f,cn){ for (var i=0;i<f.elements.length;i++){ if(f.elements[i].className==cn && f.elements[i].checked==true && f.elements[i].disabled==false){return true;} } return false; } </script> </head> <body> <form> ■分類<br> <input type="radio" value="アリ" name="fpcdata" class="fpcdata" onClick="classDisable(this.form,'Kind01',!this.checked);classDisable(this.form,'nofruit',classCheck(this.form,'Kind01'));">アリ ( <input type="checkbox" value="DXF" name="Kind01[]" class="Kind01" disabled onClick="classDisable(this.form,'nofruit',classCheck(this.form,this.className))">DXF <input type="checkbox" value="ガーバ" name="Kind01[]" class="Kind01" disabled onClick="classDisable(this.form,'nofruit',classCheck(this.form,this.className))">ガーバ <input type="checkbox" value="PDF" name="Kind01[]" class="Kind01" disabled onClick="classDisable(this.form,'nofruit',classCheck(this.form,this.className))">PDF ) <input type="radio" value="ナシ" name="fpcdata" class="fpcdata" onClick="classDisable(this.form,'Kind01',this.checked);classDisable(this.form,'nofruit',classCheck(this.form,'Kind01'));" checked>ナシ <input type="radio" value="相談" name="fpcdata" class="fpcdata" onClick="classDisable(this.form,'Kind01',this.checked);classDisable(this.form,'nofruit',classCheck(this.form,'Kind01'));"> 相談  ■分類<br> <input type="radio" value="ナシ" name="print" class="print" onClick="classDisable(this.form,'Kind04',this.checked);classDisable(this.form,'nofruit',classCheck(this.form,'Kind04'));" checked>ナシ <input type="radio" value="アリ" name="print" class="print" onClick="classDisable(this.form,'Kind04',!this.checked);classDisable(this.form,'nofruit',classCheck(this.form,'Kind04'));">アリ  ( <input type="checkbox" value="銀シールド" name="Kind04[]" class="Kind04" disabled onClick="classDisable(this.form,'nofruit',classCheck(this.form,this.className))">銀シールド <input type="checkbox" value="銀ペースト" name="Kind04[]" class="Kind04" disabled onClick="classDisable(this.form,'nofruit',classCheck(this.form,this.className))">銀ペースト <input type="checkbox" value="銅ペースト" name="Kind04[]" class="Kind04" disabled onClick="classDisable(this.form,'nofruit',classCheck(this.form,this.className))">銅ペースト <input type="checkbox" value="シルク" name="Kind04[]" class="Kind04" disabled onClick="classDisable(this.form,'nofruit',classCheck(this.form,this.className))">シルク  色 <SELECT name="Kind04[]" class="Kind04" disabled onClick="classDisable(this.form,'nofruit',classCheck(this.form,this.className))"> <OPTION SELECTED VALUE="---">--- <OPTION VALUE="白">白 <OPTION VALUE="黒">黒 <OPTION VALUE="他">他 </SELECT> ) </form> </body> </html>

専門家に質問してみよう