javascript程序,读取姓名和年龄,并在点击按钮时显示弹出消息



我是一个尝试学习javascript函数的初学者。我想制作一个网页,读取姓名和年龄,并在点击按钮时显示弹出消息,这是我的代码。我尝试了不同的具有警报方法的函数,它们运行良好,但这里的函数没有人告诉我这个函数出了什么问题?

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title> Q2 LAB2 </title>
<script>

function welcommsg() {
var username = document.getElementById("name").value;;
var userage = document.getElementById("age").value;;
var ageINT
ageINT = parseint(userage);

if ( ageINT >= 18)
{
window.alert("welcome back" + username + "we missed you ");
}
else 
window.alert("hello" + username + "we we are sorry . this website is for grown-ups only ");
}


</script>
</head>
<body>
<p> please enter your name and age </p>
<form>
name : <input id="name" type="text" />
age : <input id="age"  type="text"  />
<button onclick="welcommsg();"> enter</button>

</form>
</body>

</html>

这应该会更好地工作。每当点击网页内部的按钮时,表单标签就会重新加载网页,因为它认为你正在向服务器提交表单。这样,警报就不会显示。我还对样式和格式做了一些更改,这些更改将随着时间的推移而出现!到目前为止,这太棒了。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title> Q2 LAB2 </title>
<script>
function welcommsg() {
var username = document.getElementById("name").value;
var userage = document.getElementById("age").value;
var ageINT;
ageINT = parseInt(userage);

if ( ageINT >= 18) {
window.alert("welcome back " + username + " we missed you ");
}
else {
window.alert("hello " + username + " we we are sorry . this website is for grown-ups only ");
}
}
</script>
</head>
<body>
<p> please enter your name and age </p>
name : <input id="name" type="text" />
age : <input id="age"  type="text"  />
<button onclick="welcommsg()"> enter</button>
</body>

</html>

最新更新