ラジオボタンを使用する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>:
<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開発の真の初心者なので、どんな助けでも大歓迎です。