問題描述
點擊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