解決前臺JS彈框Alert點選確定頁面會重新整理
阿新 • • 發佈:2021-01-12
問題描述
點選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