我试图重定向时,每个无线电从两个问题检查。但是我使用的下面的代码不工作。
function chekcSubmitType()
{
if(document.getElementById('ra').checked, document.getElementById('rc').checked)
{
window.location="page-1.html";
}
if(document.getElementById('ra').checked, document.getElementById('rd').checked)
{
window.location="page-2.html";
}
if(document.getElementById('rb').checked, document.getElementById('rc').checked)
{
window.location="page-3.html";
}
if(document.getElementById('rb').checked, document.getElementById('rd').checked)
{
window.location="page-4.html";
}
}
我用JavaScript代码把这两个问题分开了。
<form name="myquiz" method="post">
<p>Question (1)</p>
<span>500</span>
<input type="radio" id="ra" value="question1"/>
<span>1000</span>
<input type="radio" id="rb" value="question1"/>
<!--I have separated these both by using a JavaScript code-->
<p>Question (2)</p>
<span>1500</span>
<input type="radio" id="rc" value="question2"/>
<span>2000</span>
<input type="radio" id="rd" value="question2"/>
<input type="button" onClick="chekcSubmitType()" value="Go" />
</form>
下面是我实际想做的:
(r)代表无线电
ra + rc redirect to page-1.html
ra + rd redirect to page-2.html
rb + rc redirect to page-3.html
rb + rd redirect to page-4.html
如果要检查两个表达式是否为真,请使用&&
-运算符,不要使用,
分隔它们:
function chekcSubmitType() {
if(document.getElementById('ra').checked && document.getElementById('rc').checked) {
window.location="page-1.html";
}
if(document.getElementById('ra').checked && document.getElementById('rd').checked) {
window.location="page-2.html";
}
// and so on...
}
有关更多信息,请参阅有关逻辑运算符的附加信息。
使用&&
操作符…
还可以使用else if
使您的代码更好…
function chekcSubmitType() {
if (document.getElementById('ra').checked && document.getElementById('rc').checked) {
window.location = "page-1.html";
}
else if (document.getElementById('ra').checked && document.getElementById('rd').checked) {
window.location = "page-2.html";
}
else if (document.getElementById('rb').checked && document.getElementById('rc').checked) {
window.location = "page-3.html";
}
else if (document.getElementById('rb').checked && document.getElementById('rd').checked) {
window.location = "page-4.html";
}
}
Vegar关于&&操作符,你也应该使用window。location。Href = "而不是window.location。(有些浏览器需要.href才能工作)
function checkSubmitType()
{
if(document.getElementById('ra').checked && document.getElementById('rc').checked)
window.location.href = 'page-1.html';
if(document.getElementById('ra').checked && document.getElementById('rd').checked)
window.location.href = 'page-2.html';
if(document.getElementById('rb').checked && document.getElementById('rc').checked)
window.location.href = 'page-3.html';
if(document.getElementById('rb').checked && document.getElementById('rd').checked)
window.location.href = 'page-4.html';
}