现在的位置: 首页 > 综合 > 正文

仿淘宝图片空间 点击文字 出现可编辑文本框 提交ajax数据到后台修改

2017年12月04日 ⁄ 综合 ⁄ 共 3855字 ⁄ 字号 评论关闭

思路来源:昨天看淘宝的图片空间,发现了一个效果.

点击宝贝的名字,出现一个可编辑的文本框,然后ajax提交(如果数据变的话),改变数据

看图说明:

编辑完了提交进行修改数据

自己做的代码效果如下:

具体设置到的代码如下:

  1. <input class="t" id="t_<?php echo $option['order_id'];?>" readonly
  2. value="<?php echo $option['delivery_number'];?>"
  3. onclick="rename('<?php echo $option['order_id'];?>',$(this).val());" />   
  4.                 <input type="button" value="Edit" style="display:none;"
  5. id="submit_<?php echo $option['order_id'];?>"
  6. onclick="xiugai('<?php echo $option['order_id'];?>','<?php echo $option['delivery_number'];?>')" />  

放置两个input,对其id进行设置不同的值,用了两个方法 rename 和 xiugai

引用的css文件为:

  1. <style type="text/css">   
  2.                   .thumb-name {   
  3.                     height20px;   
  4.                     line-height20px;   
  5.                     margin-top2px;   
  6.                     overflowhidden;   
  7.                     text-aligncenter;   
  8.                 }   
  9.                    
  10.                 input.t {   
  11.                     border:1px solid #fff;   
  12.                     background:#fff;   
  13.                 }   
  14.                 input.s {   
  15.                     border:1px solid #369;   
  16.                     background:#fff;   
  17.                 }   
  18. </style>  

两个函数为:

  1. <script type="text/javascript">   
  2.                   function rename(id,value){   
  3.                        // console.log(id + ' ' + value);
      
  4.                         var test = document.getElementById("t_" + id);   
  5.                         var mysubmit = document.getElementById("submit_" + id);   
  6.                         test.readOnly = false;   
  7.                        // test.select();
      
  8.                         test.className = "s";   
  9.                         mysubmit.style.display = "";   
  10.                   }   
  11.                   function xiugai(id,old_value){   
  12.                         //console.log(id + ' ' + old_value);
      
  13.                         //如果原始值和现在的值一样的话就不提交ajax.否则进行修改
      
  14.                         // console.log(old_value);
      
  15.                          var new_value = $('#t_'+id).val();   
  16.                         // console.log(new_value);
      
  17.                          if(old_value == new_value){   
  18.                             //console.log('不做任何修改');
      
  19.                          }else{   
  20.                             //console.log('ajax 提交');    
      
  21.                             $.ajax({   
  22.                                type: "POST",   
  23.                                url: "dj_order_delivery.php",   
  24.                                data: "id=" + id + "&value=" + new_value,   
  25.                                dataType: "html",   
  26.                                beforeSend: function(XMLHttpRequest){   
  27.                                     //$('#' + ipz_id + '_img').toggle();
      
  28.                                 },   
  29.                                 success: function(data, textStatus){   
  30.                                     //赋值
      
  31.                                     //$('#main').html(data);
      
  32.                                     //$('#' + ipz_id + '_img').toggle();
      
  33.                                     //赋值给input元素
      
  34.                                     $('#t_'+id).text(data);   
  35.                                 },   
  36.                                 complete: function(XMLHttpRequest, textStatus){   
  37.                                 },   
  38.                                 error: function(){   
  39.                                 },   
  40.                                 cache : false  
  41.                             });   
  42.                         }   
  43.                         var test = document.getElementById("t_" + id);   
  44.                         var mysubmit = document.getElementById("submit_" + id);   
  45.                         //mysubmit.onclick = function(){
      
  46.                             test.readOnly = true;   
  47.                             test.className = "t";   
  48.                             mysubmit.style.display = "none";   
  49.                         //}
      
  50.                   }   
  51.                 </script> 

《仿淘宝图片空间 点击文字 出现可编辑文本框 提交ajax数据到后台修改》由php代码分享整理编辑,转载请保留本文地址:http://www.phpsharer.com/263.html

抱歉!评论已关闭.