2012-07-18 88 views
3

我一直在編寫一個腳本,打開一個對話框,在對話框中有一個動態表,其中的數據從我的數據庫中抽出。我已將刪除按鈕放在所有記錄旁邊,以便員工可以刪除不必要的記錄。
我正在嘗試使用觸發Ajax請求的單擊函數,該請求會立即從數據庫中刪除記錄。
但是,當我點擊刪除按鈕時,它會關閉對話框並且根本不會刪除任何東西。如果有任何幫助,我已經複製了我的代碼。帶動態表刪除記錄按鈕不工作的jQuery對話框

所有幫助是極大的讚賞:)

//including all jquery library 

<script> 
    $(function() { 
     $("#dialog").dialog({ 
      autoOpen: false, 
        buttons: { 

       "Add to Log": function() { 
       $.ajax({ 
        type: 'POST', 
        url: "check_add.php", 
        data: $('#checkup').serialize(), 

        success: function(data) { 

          if(data == 0){//Failure 
            alert('Data was NOT saved in db!'); 
             } 
           } 
       }); 
        $(this).dialog("close"); 


       }, 
       Exit: function() { 
        $(this).dialog("close"); 
       } 
      } 

     }); 

      $("#opener").click(function() { 
      $("#dialog").dialog("open"); 
      return false; 
     }); 


     $("#reminder").dialog({ 
      width: 471, 
      autoOpen: false, 
        buttons: { 

       Exit: function() { 
        $(this).dialog("close"); 
       } 
      } 
     }); 

      $("#remind").click(function() { 
      $("#reminder").dialog("open"); 
      return false; 
     }); 


     $(".Delete").click(function() // here the delete function 
     { 
      var $this = $(this); 
      var rowid = $this.attr('name'); 
      $.ajax({ 
      type: "POST", 
      url: 'check_del.php', 
      data: { 
      'id': rowid 
       }, 
      success: function(data) { 

     if(data == 1){//Success 
      alert('Sucess'); 
      } 
     if(data == 0){//Failure 
      alert('Data was NOT saved in db!'); 
      } 
      } 
}); 
     }); 


    }); 
    </script> 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 

<button id="opener">Add Damage</button> 
<button id="remind">Reminder</button> 

<div id="dialog" style="font-size:small;"> 
<form name="checkup" method="post" id="checkup"> 
    <table> 
    <tr><td> 
    <strong>Cases:</strong></td><td><input name="cases" type=text placeholder="Cases ID" maxlength="7" style="width:129px;"></td> 
    </tr> 
    <tr> 
    <td><strong>Comments:</strong></td> 
    <td> <textarea name="comment" placeholder="Comments Box"></textarea></td> 
    </tr> 
    </table> 
    </form> 
</div> 

<?php 

//數據庫連接東西

$sql="select * from $tbl_name"; 
$result=mysql_query($sql); 

?> 


<div id="reminder" style="font-size:small;"> 
<form name="remind" method="post" id="remind"> 
    <table bordercolor="#000000" border="1" style="border:thin;"> 
    <th>Class</th> 
    <th>Cases</th> 
    <th>First Name</th> 
    <th>Last Name</th> 
    <th>Model</th> 
    <th>Comments</th> 
    <th> </th> 

<? 
    while($rows=mysql_fetch_array($result)){ 
$cases=strtoupper($rows['cases']); 
?> 

    <tr> 
    <td><? echo $rows['hg']; ?></td> 
    <td><? echo $cases;?></td> 
    <td><? echo $rows['firstname']; ?></td> 
    <td><? echo $rows['surname']; ?></td> 
    <td><? echo $rows['model']; ?></td> 
    <td><? echo $rows['comments']; ?></td> 
    <td><button name="<?php echo $rows['id']; ?>" class="Delete">Delete</button></td> 
    </tr> 

    </table> 
<? 
} 
?> 
    </form> 
</div> 
+0

你試過看着鍍鉻的AJAX交通(右鍵點擊任意位置上頁面,檢查元素,點擊'網絡'標籤)或在Firefox(使用螢火蟲...也選擇網絡標籤)? – 2012-07-18 06:12:05

+0

是的,我看過firefox中的流量在firefox中的東西確實顯示了一秒,但它消失之前,我可以得到一個屏幕截圖,甚至讀它。 – Matthew 2012-07-18 23:16:40

回答

0

試試這個

$(".Delete").live('click',function() { 
// your code go here 
}); 
+0

'live'已被棄用 - 嘗試在jQuery 1.7或更高版本上使用'on',並選擇使用舊版本''live''委託給'live'。 – Kelly 2012-07-18 21:43:55

+0

即時對不起凱利我不太明白你的意思? – Matthew 2012-07-18 22:32:53

+0

我已經嘗試過使用。 「開」而不是「活」,但它沒有奏效,我仍然面臨同樣的問題 – Matthew 2012-07-18 23:20:40

1

大量的測試我略微改變了我的代碼後。我已經粘貼HTML的工作部分的按鈕和jQuery代碼

HTML按鈕

<input name="<? echo $rows['id'];?>" type="button" value="Delete" /> 

jQuery函數

$("input[type='button']").on('click', function() { 

    var $this = $(this); 
    var userid = $this.attr('name'); 

      $.ajax({ 
      type: "GET", 
      url: 'check_del.php', 
      data: { 
      'id': userid 

       }, 
      success: function(data) { 

     if(data == 1){//Success 
      alert('Sucess'); 
      } 
     if(data == 0){//Failure 
      alert('Data was NOT saved in db!'); 
      } 
      } 
    }); 
    });