Jquery无刷新实时更新表格数据

本例与《jquery表格可编辑修改表格里面的值,点击td变input无刷新更新表格》差不多的功能,但是本例的更新是一次更新一行数据,具体的请看更新文件table_edit_ajax.php。

数据库当然也是相同的,

Jquery无刷新实时更新表格数据_第1张图片
演示

 

 

PHP Code
  1. "600" align="center">  
  2. class="head">  
  3. FirstLast  
  4.   
  5. $sql=mysql_query("select * from add_delete_record");  
  6. $i=1;  
  7. while($row=mysql_fetch_array($sql))  
  8. {  
  9. $id=$row['id'];  
  10. $content=$row['content'];  
  11. $text=$row['text'];  
  12.   
  13. if($i%2)  
  14. {  
  15. ?>  
  16. "" class="edit_tr">  
  17. else { ?>  
  18. "" bgcolor="#f2f2f2" class="edit_tr">  
  19.   
  20. "50%" class="edit_td">  
  21. "first_" class="text">echo $content; ?>  
  22. "text" value="" class="editbox" id="first_input_" />  
  23.   
  24. "50%" class="edit_td">  
  25. "last_" class="text">echo $text; ?>   
  26. "text" value=""  class="editbox" id="last_input_"/>  
  27.   
  28.   
  29.   
  30. $i++;  
  31. }  
  32. ?>  
  33.   
  34.   
  35. "center">

主要是js文件

 

JavaScript Code
  1. "text/javascript">  
  2. $(document).ready(function()  
  3. {  
  4. $(".edit_tr").click(function()  
  5. {  
  6. var ID=$(this).attr('id');  
  7. $("#first_"+ID).hide();  
  8. $("#last_"+ID).hide();  
  9. $("#first_input_"+ID).show();  
  10. $("#last_input_"+ID).show();  
  11. }).change(function()  
  12. {  
  13. var ID=$(this).attr('id');  
  14. var first=$("#first_input_"+ID).val();  
  15. var last=$("#last_input_"+ID).val();  
  16. var dataString = 'id='+ ID +'&content='+first+'&text='+last;  
  17. $("#first_"+ID).html('');  
  18.   
  19.   
  20. if(first.length && last.length>0)  
  21. {  
  22. $.ajax({  
  23. type: "POST",  
  24. url: "table_edit_ajax.php",  
  25. data: dataString,  
  26. cache: false,  
  27. success: function(html)  
  28. {  
  29.   
  30. $("#first_"+ID).html(first);  
  31. $("#last_"+ID).html(last);  
  32. }  
  33. });  
  34. }  
  35. else  
  36. {  
  37. alert('不能为空.');  
  38. }  
  39.   
  40. });  
  41.   
  42. $(".editbox").mouseup(function()   
  43. {  
  44. return false  
  45. });  
  46.   
  47. $(document).mouseup(function()  
  48. {  
  49. $(".editbox").hide();  
  50. $(".text").show();  
  51. });  
  52.   
  53. });  
  54.   

 table_edit_ajax.php

 

PHP Code
  1. include_once('conn.php');  
  2. if($_POST['id'])  
  3. {  
  4. $id=mysql_escape_String($_POST['id']);  
  5. $content=mysql_escape_String($_POST['content']);  
  6. $text=mysql_escape_String($_POST['text']);  
  7. $sql = "update add_delete_record set content='$content',text='$text' where id='$id'";  
  8. mysql_query($sql);  
  9. }  
  10. ?>  

 


原文地址:http://www.freejs.net/article_biaodan_50.html

你可能感兴趣的:(Jquery无刷新实时更新表格数据)