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

时间:2016-03-09 01:37:19

标签: php jquery html

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

这是我的代码:

<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> 

感谢您的帮助。 此致

2 个答案:

答案 0 :(得分:0)

由于您为点击的元素使用了链接,因此可能在您的代码运行之前尝试实际打开链接地址。使用e.preventDefault();和下面的事件处理程序来避免这种行为:

See this 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');
    }
  });


});

答案 1 :(得分:-1)

我在网上发现了这个例子...... 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>