0

ラジオボタンを使用するWordPressベースのHTMLフォームを検証するためにjavascriptを取得するのにしばらく苦労してきました。私はついに解決策を思いついたのですが、少なくともIEとChromeでは機能しましたが、Firefoxでは機能しません(これは私のコードが少しずさんなことを示唆しています)。私のラジオボタンの参照が問題だと思います。検証に非効率的なアプローチを使用することを除けば、誰かが私が間違ったことを手伝ってくれるでしょうか:-)?

私のフォームの簡略版:

<script>
function validateForm()
{
var aa=document.forms["personalise"]["motivation"]["1a"];
var ab=document.forms["personalise"]["motivation"]["1b"];
var ac=document.forms["personalise"]["motivation"]["1c"];
var ad=document.forms["personalise"]["motivation"]["1d"];
var ae=document.forms["personalise"]["motivation"]["1e"];
if (!(aa.checked == true || ab.checked == true || ac.checked == true || ad.checked == true || ae.checked == true))
 {
  alert("Question 1 must be completed");
  return false;
 }
}
</script>
<form name="personalise" action="insertdatatest.php" onsubmit="return validateForm()" method="post">
1. Are you seriously planning to quit </b>:&nbsp;&nbsp;
    <input id="1a" type="Radio" title="" name="motivation" value="1" />  Within the next 2 weeks
    <input id="1b" type="Radio" title="" name="motivation" value="2" />  Within the next 30 days
    <input id="1c" type="Radio" title="" name="motivation" value="3" />  Within the next 3 months
    <input id="1d" type="Radio" title="" name="motivation" value="4" />  No, I am not currently planning to quit
    <input id="1e" type="Radio" title="" name="motivation" value="5" />  I have already quit
<input type="submit" value = "Submit">
</form>

私はWeb開発の真の初心者なので、どんな助けでも大歓迎です。

4

2 に答える 2

0

これが物事を行うためのよりクリーンな方法です:

function validateForm() {
    var inputs = document.getElementsByName( "motivation" );
    for ( var i = 0; i < inputs.length; i++ ) {
        if ( inputs[i].checked ) return true;
    }

    alert( "Question 1 must be completed" );
    return false;
}
于 2012-10-20T09:58:56.437 に答える
0

助けてくれたTheDeadMedicにもう一度感謝します。実際、私はそれを行うためのわずかに異なる方法を見つけました。これは、複数のラジオボタンの質問用に設定された3つのブラウザーすべてで機能しました(したがって、blOKエントリ)。他の人に役立つ場合は、コードを以下に示します。Flix。

<script>
function validateForm() { 
var inputs;
var i;
var blOK;
 blOK = false;
 inputs = document.getElementsByName( "motivation" ); 
 for (i=0;i<inputs.length;i++)
 {
   if ( inputs[i].checked   ) {
                                blOK = true ;
        break ;           
                                }
}
        if (!blOK) 
        {
alert( "Question 1 must be completed" ); 
return false;  
        }
</script>
于 2012-10-22T15:16:30.617 に答える