javascript - html 警报不断将我的页面重定向到 index.html,我希望它只显示一个警报

标签 javascript html

我试图让我的拒绝按钮弹出一个警报,现在它显示警报然后将我的页面重定向到 index.php

index.php 包含在我的表单 action="index.php" 中。

注意:这是一个 .php 文件。

抱歉,如果这是一个微不足道的问题,我是新手,还在学习。

<!DOCTYPE html>
<html>
   <head>
      <!-- Meta Data -->
      <link rel="stylesheet" type="text/css" href="css/custom.css">
      <link rel="stylesheet" type="text/css" href="css/Privacy.css">
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <meta name="description" content="Privacy page">
      <meta name="author" content="groupdev17">
      <!-- Title Data -->
      <title>Privacy Policy</title>
   </head>
   <body>
      <!-- PHP including connection,header, and nav bar -->
      <?php
         include('connection.php');
         include("header.html");
         include("navBar.php");
         ?>
      <!-- class made for contents of privacy statement -->
      <div class="privacyDisplay">
         <p>
         <div class="main-content">
            <h2 id="h2TITLE">PRIVACY STATEMENT</h2>
            <br></br>
            Your privacy is important to us. This privacy statement explains the personal data that we collect and store, how we processes it, and for what purposes. In order for you to add products to a cart and to purchase products from us, we collect and store the following personal information on you:
            <br></br>
            <b>1) Name:</b><br>
            <b>2) Email:</b><br>
            <b>3) Home address:</b><br></br>
            When you complete your purchase by checking out we then collect, but do not store, the following additional information:
            Credit card information
            At no time do we give access to your personal information to third parties.
            In order to continue to use our web site, you must select Accept to indicate that you understand and accept how your personal information is being collected and stored. If you select Decline, then your account will be deactivated and your personal information will no longer be available to anyone. You may still reactivate your account at any time by accepting these terms at a later time. 
            </p>
            <!-- buttons to accept or decline privacy -->
            <form action="index.php" method="get">
            <input name = "answer" type="submit" value="Accept" onclick="acceptFunction()" id="privacyAccept">
            <input name = "answer" type="submit" value="Decline" onclick="declineFunction()" id="privacyDecline">
</form>
         </div>
      </div>
      <!-- copywrite -->
      <div class="container">
         <footer>Victoria Mobile © 2019</footer>
      </div>
      <!-- including footer -->
      <?php
         include("footer.html")
         ?>
      <!-- javascript methods for accept and decline -->
      <script>
         function acceptFunction(){
         
               }
         
         function declineFunction(){
          alert("You must accept the policy agreement to continue");
		return false;

         }
      </script>
   </body>
</html>

最佳答案

您的按钮提交表单,因此您需要通过将 false 返回到按钮的点击处理程序或将按钮的类型从提交更改为按钮来停止它。

function declineFunction() {
  alert("You must accept the policy agreement to continue");
  return false;
}
<form action="index.php" method="get">
  <input name="answer" type="submit" value="Decline" onclick="return declineFunction()" id="privacyDecline">
</form>

关于javascript - html 警报不断将我的页面重定向到 index.html,我希望它只显示一个警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55482020/

相关文章:

javascript - 浏览器中的时区差异

php - 我如何通过 Wordpress 使用 jQuery UI?

html - 如何使用 CSS 计数器编写 CSS 选择器以将格式应用于特定元素?

javascript - 无法在不弄乱整个文档的情况下调整 "boxed"文本的大小

javascript - 带有内部链接的 Bootstrap 侧边菜单在点击时跳转

html - 在 CSS 中将 Div 行转换为列

javascript - 如何使用 jQuery 删除所有点击事件处理程序?

javascript - Mixpanel - 身份验证问题,javascript

javascript - Jquery 使切换和替换多个元素

javascript - 如何在 jPlayer 中使用 .wav 音频文件