制作一个简单的 javascript 程序,根据用户的年龄和婚姻状况将用户分类到特定的保险范围内。以下程序有效,但是当我测试程序时,我无法让程序正确区分 3 类和 4 类保险。我对javascript相当陌生,所以如果我有一些愚蠢的事情,请原谅。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title> Insurance Program </title>
<script type = "text/javascript">
function insurance(form)
{
var fname = form.firstname.value;
var sname = form.surname.value;
var age = form.age.value;
var insurance;
var marital = form.maritalstatus.value;
document.write(marital);
if (marital = 2 && age >= 30)
{
insurance = "Grade 1";
}
else if (marital = 1 && age > 30)
{
insurance = "Grade 2";
}
else if (marital = 2 && age < 30)
{
insurance = "Grade 3";
}
else if (marital = 1 && age < 30)
{
insurance = "Grade 4";
}
document.write("Thank you " + fname + " " + sname + "<br>");
document.write("You belong to " + insurance + " Insurance");
}
</script>
</head>
<body>
<h4> Insurance Program </h4>
<form>
Enter your first-name : <input type = "text" name="firstname">
Enter your surname : <input type = "text" name="surname"> <br>
Enter your age: <input type = "text" name="age"> <br>
Select your current marital status
<Select id ="maritalstatus" name = "maritalstatus">
<option value = "1"> Single </option>
<option value = "2"> Married </option>
</Select>
<input type = "submit" value = "submit" onclick="insurance(this.form)">
</form>
</body>
</html>