Update data in a table with jquery and ajax

后端 未结 5 2060
灰色年华
灰色年华 2020-12-10 22:53

I am trying to update a table according to ajax respond. My update should be insert as the first row inside in my table. With my coding this is ha

相关标签:
5条回答
  • 2020-12-10 23:23

    It is because your appending the data, you should use .html(data) then it will be replaced by your new data

    0 讨论(0)
  • 2020-12-10 23:30

    use html instead of append

    success: function(data) {
            $('#manage_user table > tbody:first').html(data);
            //alert(data);
    }
    
    0 讨论(0)
  • 2020-12-10 23:38

    here the solution for you

    OLD :

    $('#manage_user table > tbody:first').append(data);
    

    NEW :

    $('#manage_user table > tbody').prepend(data);
    

    You need to use prepend at the place of append

    0 讨论(0)
  • 2020-12-10 23:41
    $('#manage_user table > tbody:last').find('tr:first').before(data);
    

    Try this. check my fiddle : http://jsfiddle.net/W4gYY/3/

    If you declared thead then you can use tbody:first and working fine. You do not mention thead that is way html treated as default tbody

    If your html look like below :

    <div id="manage_user">
    <table>
      <thead>  
      <tr>
        <th><input type='checkbox' class='selectAll' name='selectAll' value='' /> Name</th>
        <th>Address</th>
        <th>City</th>
        <th>Edit</th>
        <th>Delete</th>
      </tr>
      </thead>
      <tbody>
         <tr>
          <td><input type='checkbox' name='' value='' class='' />&nbsp;&nbsp;sdfsdfs</td>
          <td>dsfs</td>
          <td>dsfdsf</td>
          <td><span class='edit_ico'></span></td>
          <td><span class='delete_ico'></span></td>
         </tr>
         <tr>
          <td><input type='checkbox' name='' value='' class='' />&nbsp;&nbsp;aaaaaaa</td>
          <td>dfsdf</td>
          <td>dsfsf</td>
          <td><span class='edit_ico'></span></td>
          <td><span class='delete_ico'></span></td>
         </tr>
     </tbody>
    </table>
    </div>
    

    then you can use

    $('#manage_user table > tbody:first').find('tr:first').before(data);
    

    otherwise without thead in html you have to do following code

    $('#manage_user table > tbody:last').find('tr:first').before(data); 
    
    0 讨论(0)
  • 2020-12-10 23:49

    There is a jQuery method called after() and it can do the thing you want...

    $('#manage_user table > tbody:first').append(data); 
    

    can be changed to

    $('#manage_user tr:first').after('<tr><td>first</td></tr>'); which inserts the result as first row of the table... Check the demo here

    0 讨论(0)
提交回复
热议问题