美文网首页
JS示例33-表单事件(提交和重置事件)

JS示例33-表单事件(提交和重置事件)

作者: 微小码 | 来源:发表于2019-04-05 16:15 被阅读0次

一、知识要点

onsubmit
onreset

二、源码参考

<!DOCTYPE HTML>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>无标题文档</title>
    <script>
        window.onload = function () {

            var oForm = document.getElementById('form1');

            // onsubmit : 当表单被提交的时候触发
            oForm.onsubmit = function () {
                if (this.username.value == '') {
                    alert('请填写用户名');
                    return false;
                }
            }

            // onreset : 当表单要重置的时候触发
            oForm.onreset = function () {
                return confirm('你确定要重置?');
            }

            confirm('你确定要重置?');
            document.title = 1;

        }
    </script>
</head>

<body>
    <form id="form1" action="http://www.baidu.com">
        <input type="text" name="username" />
        <input type="password" name="password" />
        <!--<input type="button" value="提交" />-->
        <input type="submit" value="提交" />
        <input type="reset" value="重置" />
    </form>
</body>

</html>
image.png

相关文章

网友评论

      本文标题:JS示例33-表单事件(提交和重置事件)

      本文链接:https://www.haomeiwen.com/subject/zulabqtx.html