【JavaScript】解决前台JS弹框Alert点击确定页面会刷新

问题描述

点击alert弹出的对话框中的确定按钮之后页面自动刷新,怎样防止不刷新?

示例:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="jquery-3.5.1/jquery-3.5.1.js"></script>
    <title></title>
    <script>
        function send() {
            var name = $('#name').val();
            if (name == '') {
                alert('name不能为空')
                return false;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input id="name" type="text" />
            <button onclick="send();" type="button">按钮</button>
        </div>
    </form>
</body>
</html>

有人说:在alert 后面 写一个 return false 页面就不会刷新了;还有人说:正常情况 alert 之后页面是不会刷新的,应该是做了刷新的动作,然而并没有什么卵用。

点击按钮的时候,执行send()方法,弹出alert提示框,然后页面就自动刷新了。原因是button缺少type类型,加上 type=“button” 就好了,本以为button本身就是按钮,不需要再声明它的类型了,这才导致了这次的问题。

 

原文链接:https://blog.csdn.net/bxj19890514/article/details/82771380

 

posted @ 2021-01-12 15:02  智者见智  阅读(2885)  评论(0编辑  收藏  举报