很多時候需要修改資料,就用jq做了個輕按兩下文字能實作修改并儲存資料的 部分代碼如下: XML/HTML代碼 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>測試用的</title> <script language="javascript" src="demo2/jquery-1.3.2.min.js"></script> </head> <body> <table width="66%" cellspacing="0" cellpadding="0"> <tr> <td height="29" align="center"><strong>使用者名</strong></td> <td align="center"><strong>郵件位址</strong></td> </tr> <tr> <td class="unm" uid=1 id="unm1" filed="unm">使用者A</td> <td class="email" uid=1 id="email1" filed="email">[email protected]</td> </tr> <tr> <td class="unm" uid=2 id="unm2" filed="unm">使用者B</td> <td class="email" uid=2 id="email2" filed="email">[email protected]</td> </tr> </table> </body> </html> 其中html代碼中 td标簽中有2個自定義屬性,uid,filed ,第一個uid用來表示資料庫中對應表中的id字段,filed用來表示這個td裡面的資料代表表中哪個字段的 JavaScript代碼 <script language="javascript"> $().ready(function(){ $(".unm,.email").dblclick(function(){ id=$(this).attr("uid"); value=$(this).text(); f=$(this).attr("field"); text_id=$(this).attr("id"); if(value) { $(this).html("<input type='text' id="+id+" name="+f+" value="+value+">"); $(".unm > input,.email>input").focus().blur(function(){ $.ajax({ type: "POST", url: "save.php", data: "id="+id+"&type="+f+"&value="+$("#"+id).val(), success: function(msg){ $("#"+text_id).text(msg); } }); }) } }) }) </script> save.php <?php //儲存到資料庫 if($_POST["type"]=="email") { //mysql_query("update sometable set email='{$_POST["value"]}' where id={$_POST["id"]}"); } if($_POST["type"]=="unm") { //mysql_query("update sometable set unm='{$_POST["value"]}' where id={$_POST["id"]}"); } echo $_POST["value"]; ?> |