为什么用onclick触发得了事件而onsubmit不行?
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
window.onload=function(){
var a=document.getElementById("w");
a.onsubmit=function(){
alert("登录成功");
}
}
</script>
<style type="text/css">
.box{
width: 250px;height: 150px;background: skyblue;
position: absolute;left: 0;right: 0;top: 0;bottom: 0;
margin: auto auto;
padding: 20px 0 0 20px;
}
#w{
position: absolute; left:50%;transform: translateX(-50%);
margin-top:20px;
}
</style>
</head>
<body>
<div class="box">
<span>账号:</span> <input type="text" placeholder="请输入账号"><br/><br/>
<span>密码:</span> <input type="password" placeholder="请输入密码"><br/>
<input type="submit" value="登录" id="w">
</div>
</body>
</html>
正在回答
同学你好,onsubmit事件要绑定在form标签上才有效果,如下:
效果:
如果我的回答帮到了你,欢迎采纳,祝学习愉快~
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧