微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery+php实现双击编辑并保存数据

很多时候需要修改数据,就用jquery做了个双击文字能实现修改并保存数据的

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%" border="0" 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里面的数据代表表中哪个字段的

jquery代码:

<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"];

?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐