Home > Web Front-end > JS Tutorial > jquery ajax Double-click the div to directly modify the content in the div_jquery

jquery ajax Double-click the div to directly modify the content in the div_jquery

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 15:12:19
Original
1386 people have browsed it

Recently when I was doing backend function development, I used to modify the sorting field. I felt that it was troublesome to re-enter the editing page just to modify a sorting value, so I found some information online and wrote a jquery double-click implementation. The effect of directly modifying the sorting value:

html code:

<div title="【双击可直接修改】" class="changeSort" id="{$id}">{$sort}</div>
Copy after login

JS code:

<script type="text/javascript">
//双击修改排序
 $('.changeSort').dblclick(function(){
  var url = "{:U('setSort')}";
  var td = $(this);
  var id = td.attr('id');
  var text = td.text();
  var txt = $("<input type='text' class='input-small' >").val(text);
  txt.blur(function(){
   // 失去焦点,保存值。于服务器交互自己再写,最好ajax
   var newText = $(this).val();
   $.ajax({
     url:url,
     type:'POST',
     data:{'tid':id,'sort':newText},
     dataType:'json',
     success:function(res){
      if(res.flag==1){
      layer.msg(res.msg);
      // 移除文本框,显示新值
      $(this).remove();
      td.text(newText);
      }else if(res.flag==3){
       layer.msg(res.msg);
       txt.val(newText);
      }
     }
    });
    
  });
  td.text("");
  td.append(txt);
 });
</script>
Copy after login

PHP code:

<&#63;PHP
/**
  * ajax 设置排序值
  */
 public function setSort(){
  if(IS_POST){
   $tid = I('post.tid');
   $sort = I('post.sort');
   if(!is_numeric($sort)){
    $arr = array(
     'flag'=>3,
     'msg'=>'请输入数字',
     'link'=>'',
     'content'=>''
    );
    $this->ajaxReturn($arr);
   }
   $data = array(
    'id'=>$tid,
    'sort'=>$sort
   );
   $this->mod_sort = M('Sort');
   $res = $this->mod_sort->save($data);
   if($res){
    $arr = array(
     'flag'=>1,
     'msg'=>'排序值设置成功',
     'link'=>'',
     'content'=>''
    );
   }else{
    $arr = array(
     'flag'=>2,
     'msg'=>'排序值设置失败',
     'link'=>'',
     'content'=>''
    );
   }
  }else{
   $arr = array(
    'flag'=>0,
    'msg'=>'请求非法!',
    'link'=>'',
    'content'=>''
   );
  }
  $this->ajaxReturn($arr);
 }
&#63;>
Copy after login

The effect is as follows:

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template