基本的に、クイズのような構造を持つページを作成し、ユーザーがある質問から 1 つのオプションを選択し、別の質問から別のオプションを選択できるようにしたいと考えています。各質問への回答に応じて、特定の出力を警告する必要があります。私は Javascript の基本的な知識しか持っていないので、テキスト ボックスを使用して回答を選択するのと同じ理論を使用していますが、今回はラジオ ボタンを使用したいと考えています。
これは、ラジオ ボタンのあるページの HTML コードです。
<p>
<strong>1. This is the first question?</strong><br />
<input type="radio" name="op1" value="anw1">Option 1<br>
<input type="radio" name="op1" value="anw2">Option 2
</p>
<p>
<strong>2. This is the second question?</strong><br />
<input type="radio" name="op2" value="anw1">Option 1<br>
<input type="radio" name="op2" value="anw2">Option 2
</p>
<input type='button' value='Get result!' onClick='Submit()' /><br />
次に、次のように head タグに関数を入れます。
function Submit()
{
var op1 = document.getElementsByName('op1')
var op2 = document.getElementsByName('op2')
if(op1== "anw1" && op2== "anw1")
{
alert ("This would be if both options one were selected")
}
else if (op1== "anw2"&& op2== "anw2")
{
alert ("This would be if the the seconds ones were selected")
}
else
{
alert ("This would be if neither were true")
}
}
しかし、私は仕事をすることができないようです. 私は他の部分を動作させることができますが、他の部分は動作しません。