jquery Ajax Double-click div to directly modify the contents of the Div _jquery

Source: Internet
Author: User

Recently in the background function development, used to modify the sort field, feel just to modify a sort value, and to re-enter the edit page more trouble, so the Internet to find some information on their own hands to write a jquery double click to achieve the effect of directly modifying the sorted values:

HTML code:

<div title= "" Double-click to modify directly class= Changesort id= "{$id}" >{$sort}</div>

JS Code:

<script type= "Text/javascript" >
//Double-click Modify Sort
 $ ('. 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 () {
   //lose focus, save value. Write to the server interactively, preferably 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);
      Remove the text box and display the new value
      $ (this). Remove ();
      Td.text (NewText);
      } else if (res.flag==3) {
       layer.msg (res.msg);
       Txt.val (NewText)
  ;}}); Td.text ("");
  Td.append (TXT);
 });
</script>

PHP Code:

? PHP
/**
  * Ajax Set Sort value */public
 function Setsort () {
  if (is_post) {
   $tid = I (' Post.tid ');
   $sort = I (' Post.sort ');
   if (!is_numeric ($sort)) {
    $arr = array (
     ' flag ' =>3,
     ' msg ' => ' Please enter a number ', '
     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 ' => ' sort value set successfully ',
     ' link ' => ',
     ' content ' = > "
    );
   else{
    $arr = array (
     ' flag ' =>2, '
     msg ' => ' sort value setting failed ', '
     link ' => ',
     ' content ' => ')
    );
   }
  } else{
   $arr = array (
    ' flag ' =>0,
    ' msg ' => ' request illegal!) ', '
    link ' => ',
    ' content ' => '
   );
  }
  $this->ajaxreturn ($arr);
>

The effect of the following figure:

The above is the entire content of this article, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.