按下删除按钮时发出警报不起作用


Alert when delete button is pressed is not working

我正在做库存跟踪系统,当按下删除按钮时,我希望有一个警报说"你确定吗?当您按删除但警报未显示时,它会起作用。

这是我的代码:

<html>
<head>
  <title>View Records</title>
</head>
<body>
<p><a href="Search_Vendor.php"><img src="img/SEARCH2.jpg" width="25" 
height="27" align='left'></a></p>

<div class="dropdown"><img class="dropbtn" img src="img/dropdown.png"    
width="44" height="47"></img>
<div class="dropdown-content">
<a href="home3.php">HOMEPAGE</a>
<a href="New_Vendor.php">ADD NEW RECORD</a>
<a href="Print_Vendor.php">PRINT</a>
</div>
</div> 
<center> <h2> VENDOR RECORD </h2> </center>
<?php
// connect to the database
include('connection.php');
// get results from database
$result = mysql_query("SELECT * FROM vendor") 
    or die(mysql_error()); 
// display data in table
echo "<table border='1' cellpadding='20' cellspacing='0' align ='center'";
echo "<tr>  <th>Vendor ID</th> <th>Vendor Name</th> <th>Contact No </th>   
<th>Address</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
    // echo out the contents of each row into a table
    echo "<tr>
                <td align = center>".$row['Vendor_ID']."</th> 
                <td align = center>".$row['Vendor_Name']."</th>
                <td align = center>".$row['Contact_No']."</th> 
                <td align = center>".$row['Address']."</th> 
                <td><a href='Update_Vendor.php?ID=$row[ID]
                &Vendor_ID=$row[Vendor_ID]
                &Vendor_Name=$row[Vendor_Name]
                &Contact_No=$row[Contact_No]
                &Address=$row[Address]'>Update</a></td>
                <td>
                <a onclick="return confirm('Are you sure?')"          
                href="Delete_Vendor.php?ID=<?= $row[ID] ?>">Delete</a>
                </td>
        </tr>";
}   
//close the table
echo "</table>";
?>
</p>
</body>
</html> 

感谢您的帮助。问候

由于您正在为单击的元素使用链接,因此它很可能正在尝试在代码运行之前实际打开链接地址。使用 e.preventDefault(); with 和事件处理程序,如下所示以避免该行为:

看这个 jsFiddle

<a class="delete" href="Delete_Vendor.php?ID=123">Delete</a>
$(function() {
  $('.delete').click(function(e) {
    var $this=$(this);
    e.preventDefault();
    if (confirm('Are you sure?')) {
      // code to delete stuff
      window.location=$this.attr('href');
    }
  });

});

我在网上找到了这个例子......http://www.w3schools.com/jsref/met_win_confirm.asp

<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
    var txt;
    var r = confirm("Press a button!");
    if (r == true) {
        txt = "You pressed OK!";
    } else {
        txt = "You pressed Cancel!";
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>