在JS中写form表单的提交与判断

 
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>form表单提交</title>
</head>
<body>
<form action="JS09.html" method="post" id="f">
    <input type="text" id="a">
    <input type="submit" value="提交" />
</form>
</body>
<script>
    var f = document.getElementById('f');
    //输入框获取
    var a = document.getElementById('a');
    //提交表单的事件监听
    f.onsubmit = function (){
        //判断是否存在内容
        if(a.value==''){
            alert('内容为空,请填写用户名');
            //阻止提交表单
            return false;
        }
        else {
            alert('提交成功')
            //可以提交表单
            return true;
        }

    }
</script>
</html>


猜你喜欢

转载自blog.csdn.net/qq_37768482/article/details/76099603