HTML警告框一直将我的页面重定向到index.html,我希望它只显示一个警告。

4

我想让我的“拒绝”按钮只是弹出一个警告框,目前它会显示警告,然后重定向到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>


好问题,我认为这是在他们职业生涯中大多数开发人员在某个时候都会遇到的问题。 - dmoore1181
4个回答

3

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

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>


我认为你的代码片段中应该是 <input name="answer" type="button" - glhr
@glhr,实际上我并没有,但你可以看到在我的答案中我提供了这个作为另一种选择。 - j08691
1
我的错。我以为代码片段中的HTML部分是用来说明第二个选项的。 - glhr

1
只需从“拒绝”按钮中删除type="submit"。改用type="button"即可:
<input name="answer" type="button" value="Decline" onclick="declineFunction()" id="privacyDecline">

你已经得到了答案,非常简单易懂。谢谢G。 - Karanvir1
1
@Karanvir1 https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work - user47589

1
如果您不想提交表单,请使用按钮而不是类型为“submit”的输入。这样,按钮将执行您与其关联的操作,而不是提交表单。
<button type="button" value="Decline" onclick="declineFunction()" id="privacyDecline">

-1

你可以使用preventDefault

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接