2016-07-22 24 views
0

我不能將ID發送給PHP刪除的行,我得到這個錯誤廣東話刪除或發送數據到PHP

TypeError: 'checkValidity' called on an object that does not implement interface HTMLButtonElement.

如果我這樣做腳本

jQuery(document).on("click",".del_beneficiaries",function(){ 
      var id = jQuery(this); 
      jQuery.ajax({ 
       type: "POST", 
       dataType: "json", 
       url: "ben_del.php", 
       data: { 
        "id" : id 
       } 
      }); 
     }); 

,但如果這樣做這腳本他們是沒有數據發送到我的PHP

jQuery(document).on("click",".del_beneficiaries",function(){ 
      var id = []; 
      jQuery(".beneficiaries_rows").each(function(){ 
       var rows = jQuery(this).attr("data-id"); 
       id.push(rows); 
      }); 
      jQuery.ajax({ 
       type: "POST", 
       dataType: "json", 
       url: "ben_del.php", 
       data: { 
        "id" : id 
       } 
      }); 
     }); 

另外,如果我讓這個腳本仍然沒有數據發送到PHP

jQuery(document).on("click",".del_beneficiaries",function(){ 
      var id = []; 
      jQuery(".data-id").each(function(){ 
       var rows = jQuery(this).closest(".beneficiaries_rows"); 
       id.push(rows); 
      }); 
      jQuery.ajax({ 
       type: "POST", 
       dataType: "json", 
       url: "ben_del.php", 
       data: { 
        "id" : id 
       } 
      }); 
     }); 

這是HTML

<table style="border: 2px solid black;margin:auto;"> 
    <tr> 
     <th width="100%"><center>Name<center></th> 
     <th><center>Action</center></th> 
    </tr> 
    <?php 
     while($row1 = mysql_fetch_array($result1)){ 
     echo "<tr class='beneficiaries_rows' id='".$row1['id']."' value='".$row1['ben_id']."'>"; 
     echo "<td>".$row1['name']."</td>"; 
     echo "<td>"; 
     echo "<center><button class='del_beneficiaries'>X</button><center>"; 
     echo "</td>"; 
     echo "</tr>"; 
     } 
    ?> 
</table> 
+0

你正在推動的jQuery對象轉換成數組發送數據。當jQuery嘗試序列化它們時會中斷。你想要發送什麼?你有選擇器不匹配HTML – charlietfl

+0

@charlietfl即時通訊發送ben_id到我的PHP刪除它的名稱 – reyjoel

回答

0

我得到了答案,我需要先撥打該行並給它一個ben_id(主鍵)就這樣

jQuery(document).on("click",".del_beneficiaries",function(){ 
      var row = jQuery(this).closest(".beneficiaries_rows"); //call the rows 
      var id = row.attr("data-id"); //give the rows the specific ben_id 
      jQuery.ajax({ 
       type: "POST", 
       dataType: "json", 
       url: "ben_del.php", 
       data: { 
        "id" : id 
       } 
      }); 
     }); 
1

您需要通過它的類指TR元素,然後檢索ID 。我建議你閱讀jQuery文檔[http://api.jquery.com/]。 另外,您可以在代碼中使用$代替JQuery。 也許這是一個家庭作業,我不能給你完整的解決方案。

提示:$(」一流)ATTR(...

+0

即時通訊使用jquery ui – reyjoel

+0

但我的html是正確的?我應該改變腳本嗎? – reyjoel

+0

你給的鏈接是**找不到** – reyjoel

1

您正在使用jQuery的click事件不同的類(del_beneficiaries)和您刪除ID存儲在不同的類(beneficiaries_rows)。我用你的HTML靜態數據和你首先JS腳本獲取ID刪除行。

您的HTML:

<table style="border: 2px solid black;margin:auto;"> 
<tr> 
    <th width="100%"><center>Name<center></th> 
    <th><center>Action</center></th> 
</tr> 
<?php 
    //while($row1 = mysql_fetch_array($result1)){ 
    echo "<tr class='beneficiaries_rows' id='1' value='22'>"; 
    echo "<td>".'Ravi'."</td>"; 
    echo "<td>"; 
    echo "<center><button class='del_beneficiaries'>X</button><center>"; 
    echo "</td>"; 
    echo "</tr>"; 
    //} 
?> 

腳本:

<script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
<script> 
jQuery(document).on("click",".del_beneficiaries",function(){ 
     var id = jQuery('#1').attr('value'); 
     alert(id); 
     jQuery.ajax({ 
      type: "POST", 
      dataType: "json", 
      url: "ben_del.php", 
      data: { 
       "id" : id 
      } 
     }); 
    }); 

+0

我沒有得到你的觀點 – reyjoel