简体   繁体   中英

Ajax Delete,Cant focus On the Specific Row

At the folloing code,I try to delete a row using ajax .serialize() but it only deletes the first row.Using jQuery(this).closest('form').find('input[name="id"]').val(); also returns "Undefined" for ID.

Ajax Code

 function AjaxDelete() {
 var rowId = $("#sil").serialize();
    var confirmation = confirm("Are you sure of deleting the following user:"+rowId);
    if (confirmation) {
      $.ajax({
          type:'POST',
          url:'sil.php',  //deleting file
          data: rowId,
          success:function(cevap){
              alert("User has been successfully removed.ID="+rowId);
          }
      });
    }
    return confirmation;
};

Table Structure

 echo '<table id="kullanicilar" align=center>
        <thead>
        <tr>
            <td></td>
            <td>ID</td>
            <td>Kullanıcı Adı</td>
            <td>Yönetici</td>
            <td colspan=4>İşlemler</td>
        </tr>
        </thead>
        ';

    while($results->fetch()){ //fetch values

        echo '<tr>
                <td><input type="checkbox" name="select[]" value="'.$id.'"></td>
                <td>'.$id.'</td>
                <td>'.$kullanici_adi.'</td>
                <td>'.$yonetici.'</td>
                <td><a href="#" onclick="return AjaxDelete();" class="link"><form method="post" id="sil"><input type="hidden" name="id" value="'.$id.'" class="id"></form><img src="img/delete.png" title="Sil"></a></td>
                <td><a href="#"><img src="img/edit.png" title="Düzenle"></img></a></td>
                <td><a href="#" class="gor">Gönderilerini Gör</a></td>
                <td><a target="_blank" href="#" class="gor">Profilini Gör</a></td>
              </tr>
             '
              ;
    }

    echo '</table><br/>';

In your while loop, you are giving same id to inputs which is wrong. you can try:

<a href="#" onclick="return AjaxDelete("'.$id.'");" class="link"><form method="post" id="sil">

and then in your ajax:

function AjaxDelete(x) {
 var rowId = x;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM