请老师帮忙检查一下代码
来源:2-13 编程练习
DB1时间的玫瑰
2021-07-29 15:41:04
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>location对象</title>
</head>
<body>
<form>
<p>
用户名:
<input type="text" id="username">
</p>
<p>
密码:
<input type="password" id="password">
</p>
<p>
<button id="btn">提交</button>
</p>
</form>
<script>
// 得到元素
var username = document.getElementById('username');
var password = document.getElementById('password');
var btn = document.getElementById('btn');
// 绑定事件
btn.onclick = function() {
// 判断是否满足提交条件
if(username.value == '张三' && password.value == '123456') {
window.location = 'https://www.imooc.com';
} else {
alert('不对');
}
}
</script>
</body>
</html>
1回答
同学你好,代码实现效果出现问题,原因如下:
HTML代码中,button按钮没有设置type属性,如果button按钮不设置type属性,则在IE浏览器部分版本中会被默认为 "button"(普通按钮),但在其他浏览器中会被默认为 "submit"(提交按钮),而“submit”提交按钮在form表单中会有默认提交行为。
所以,如果在谷歌浏览器中打开该网页的话,需要给button按钮设置type属性为“button”,参考如下

祝学习愉快!
相似问题