Welcome 微信登录

首页 / 脚本样式 / JavaScript / jQuery+ajax实现鼠标单击修改内容的方法

现有表格中的一行的代码如下所示: 

<tr> <td><span class="catid">2</span></td> <td>公司介绍</td> <td>内部栏目</td> <td><span class="listorder" title="点击修改">2</span></td></tr>
要实现鼠标单击修改内容思路如下:
 
1、点击栏目排序栏目中的数字,获取同一行的第一列中的内容,即栏目id
2、隐藏栏目排序中的数字
3、在栏目排序列中插入input框,并在input框中显示栏目排序中的内容,并设置为焦点
4、修改input中的内容,失去焦点的时候提交数据,用ajax向服务器传递数据 方法为post方法
5、提交数据的时候,友好提示修改中。。。 或者等待图片
6、返回成功信息 ,重新显示修改后的内容 去掉input框
实现这一功能的jquery核心代码如下:

$(".listorder").click(function(e){ var catid = $(this).parent().siblings("td:eq(0)").text();//获取同一行上 第一列中的id值 var listorder_now_text = $(this).text();//获取listorder中的内容 先保存起来 $(this).text("");//设置内容为空 var list_form = "<input type="text"value=""+listorder_now_text+"" size=2 class="listorder_input" />" ; $(this).parent().append(list_form); //插入 input框 $(".listorder_input").focus();//自定义一个div 提示修改中 var loading = "<div id="loading"><img src="img/loading.gif" alt="修改中..."/></div>"; $(this).parent().append(loading); $("#loading").css({ "color" : "red" , "display" : "none"})//定义ajax的全局事件 $(this).ajaxStart(function(){$("#loading").show(); }) $(this).ajaxStop(function(){$("#loading").remove(); }) $(".listorder_input").blur(function(){var thislist = $(this).siblings(); //取得同级的标签 即 修改后需要显示的 listorder$.post("ajax.php",{action : "mod_listorder",catid : catid ,listorder : $(this).attr("value")} , function(data, textStatus){$(thislist).text(data);});//end .post$(this).remove(); })//end function blur})// end function click
ajax.php中内容就简单了,这里只做处理做演示用,并没有向服务器提交数据,代码如下:
sleep(1);//延时运行1秒,查看效果用,实际代码中不需要echo $_POST["listorder"];