提交按钮不能在div中工作(其内容在jQuery中使用.load()加载)

时间:2016-04-17 04:01:56

标签: javascript php jquery

cart.php items.php以下代码显示用户购物车中的产品。使用提交按钮的用户可以删除购物车中的各个项目。它保存在cart.php文件中。此代码有效细

<?php
session_start();
?>
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
</head>
<body>
    <div class="txt-heading">Shopping Cart</div>
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><strong>Name</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>

</tr>   
 <?php
        $servername = "localhost";
$username = "root";
$password = "";
$dbname = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

      $ip = $_SERVER['REMOTE_ADDR'];

         $sql = "SELECT * FROM cart where ip='".$ip."'";
         $result = mysqli_query($conn, $sql);
    $item_total =0;

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        ?><form method="post" action="" id="cart1">
        <tr> 
        <td><strong><?php echo $row["name"]; ?></strong></td>
        <td><?php echo $row["qty"]; ?></td>
        <td align=right><?php echo "Rs.".$row["price"]; ?></td>
  <input name="no" type="hidden" value="<?php echo $row["orderno"] ?>" />   
 <td><input type="submit" name="submit" id="submit"/></td>
  </tr>
    </form>

                <?php
       $item_total += ($row["price"]*$row["qty"]);
        }
 } 







 ?> 
 <?php if ($_POST['submit']) {
    $or=$_POST['no'];
     $sql = "delete FROM cart where orderno='".$or."'";
         $result = mysqli_query($conn, $sql);
    header("Location: ".$_SERVER['PHP_SELF']);}
    ?>

<tr>

<td colspan="5" align=right><strong>Total:</strong> <?php echo     "Rs.".$item_total; ?></td>
</tr>
</tbody>
</table>        

</body>
</html>

问题是当我将其加载到另一个页面items.php的div中时 使用

  <script>
$(document).ready(function(){
setInterval(function(){
$("#cart").load('cart.php')
}, 20);
});

</script>

cart.php正在div #cart中正确加载,但是当我点击提交时没有任何反应。请注意,点击cart.php中的提交后,产品已从数据库中删除。

有人可以解释一下如何解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

我假设点击处理程序已附加但DOM(您的情况下的提交按钮)不存在,因此您可能需要delegate这样的click事件

$('body').on('click','#submit',function(event){ // submit is id of submit button
  // rest of the code
})

答案 1 :(得分:0)

我终于解决了这个问题。不知道这是否是正确的&#34;做的方式,但它对我有用。我在这里发布代码,以便它可能对面临类似问题的其他人有用

in cart.php

&#13;
&#13;
<form method="post" action="cart.php" id="cart1">
  <tr> 
   <td><strong><?php echo $row["name"]; ?></strong></td>
   <td><?php echo $row["qty"]; ?></td>
   <td align=right><?php echo "Rs.".$row["price"]; ?></td>
   <td><input  type="submit" name="<?php echo $row["orderno"] ?>" id="submit" value="Remove"/></td>
   </tr>
    </form>
    
&#13;
&#13;
&#13;

脚本被修改为

&#13;
&#13;
<script>
$(document).ready(function(){
setInterval(function(){
$("#cart").load('cart.php')
}, 200);
});
        
        
        $('#cart').on('click','#submit',function(event){ 
          var no = $(this).attr('name');
            var b = parseInt(no);
            $.ajax({
        type: 'POST',
        url: 'dlt.php',
        data: {no: b},
            });
               
});
        
        
</script>
&#13;
&#13;
&#13;

和dlt.php是

&#13;
&#13;
<?php
        $servername = "localhost";
$username = "root";
$password = "";
$dbname = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
        

    $or=$_POST['no'];
     $sql = "delete FROM cart where orderno='".$or."'";
         $result = mysqli_query($conn, $sql);
  
   
?>
&#13;
&#13;
&#13;