0

我想创建一个小型调查/问卷,询问用户他们是否超过 18 岁。一旦他们按下 Enter 按钮,我想做两个选项。一:如果他们未超过 18 岁,它会将他们带到另一个网页。第二:如果他们超过 18 岁,它会将他们带到另一个网页。这让我想起了 Java 中的“if”“else”语句,但我知道这不是 Java。我正在使用 HTML (Dreamweaver),但我无法弄清楚。有人可以指出我正确的方向吗?

我已经得到了它的基本信息:

<!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=utf-8" />
<title>WAIT!</title>
</head>

<body>
<p>WAIT!</p>
<p>Let us know more about you.</p>
<p>Please fill out this form so that we can guide you to YOUR world. </p>
<p>&nbsp;</p>
<p>Are you a:</p>
<form>
<input type="radio" name="sex" value="male">Male<br>
<input type="radio" name="sex" value="female">Female
</form> 
<p> Age: </p>
<form>
<input type="radio" name="sex" value="male">
younger than 18<br>
<input type="radio" name="sex" value="female">
18+
</form> 
<p></p>
<form name="input" action="#" method="get">
<input type="submit" value="Enter">
</form> 
</body>
</html>

要查看输出,请复制并粘贴上面的代码并在此处输入。然后按提交代码。

4

2 回答 2

1

这是一个示例脚本,可重定向用户检查使用 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=utf-8" />
<title>WAIT!</title>
</head>
<script>
function redirect() {
    if(document.getElementById("m").checked == true)
    {
        window.open("http://www.codeforbrowser.com")
    }
    else {
        window.open("http://www.yahoo.com")
    }

}
</script>
<body>
<p>WAIT!</p>
<p>Let us know more about you.</p>
<p>Please fill out this form so that we can guide you to YOUR world. </p>
<p>&nbsp;</p>
<p>Are you a:</p>
<form>
<input type="radio" name="sex" value="male">Male<br>
<input type="radio" name="sex" value="female">Female
</form> 
<p> Age: </p>
<form>
<input type="radio" name="sex" value="male" id="m" >
younger than 18<br>
<input type="radio" name="sex" value="female"  id="f">
18+ 
</form> 
<p></p>
<form name="input" action="#" method="get">
<input type="submit" value="Enter" onclick="redirect();">
</form> 
</body>
</html>

演示

于 2012-11-23T04:41:22.967 回答
-1

给你的表格一些id;

<form id="sex_form">
<input type="radio" name="sex" value="male">
younger than 18<br>
<input type="radio" name="sex" value="female">
18+
</form> 
<input type="submit" onclick="validate()" value="Enter">

    <script type="text/javascript">
        function validate(){
            var form = document.getElementById('sex_form');
            if(form.firstElementChild.checked){
            alert('to younger');
                            //window.open('younger');
            }
            else if(form.firstElementChild.nextElementSibling.nextElementSibling.checked){
                            //window.open('older');
            alert('to 18+');
           }
            else
            alert('none selected');
        }


    </script>
于 2012-11-23T04:47:43.430 回答