简体   繁体   English

用文本框替换表单元格,读取键入的值,然后使用jquery用新值替换表单元格

[英]replace a table cell with the text box, read the value typed and replace back the table cell with the new value using jquery

I am new to Jquery and working on a small project with PHP, and Jquery. 我是Jquery的新手,正在使用PHP和Jquery进行小型项目。 Below is on of the table cell. 以下是表格单元格中的。 I would like to replace this cell with a text box and read the vale entered by user and bring back the original cell(replaced cell) with this new value. 我想用一个文本框替换此单元格,并读取用户输入的标签,并用此新值带回原始单元格(已替换的单元格)。 I am not sure how can I do this with Jquery. 我不确定如何使用Jquery做到这一点。 Any help is highly appreciated. 非常感谢您的帮助。

<td><a href="#"><?php echo money_format('%.2n', $Cashup['actual_cash']); ?></a></td>

Thanks, Bhaskar 谢谢,Bhaskar

I assume in your example you want to replace the a temporarily (you wouldn't want to replace the td , the table would go funny). 我假设在您的示例中,您想临时替换a (您不想替换td ,该表会变得很有趣)。

Here's a quick version: 这是一个快速版本:

$("#theTable").delegate("td > a", "click", function(event) {
  var a, parent, input, doneLink;

  // The `a` has been clicked; cancel the action as
  // we're handling it
  event.preventDefault();
  event.stopPropagation();

  // Remember it and its parent
  a = $(this);
  parent = a.parent();

  // Insert an input in front of it, along with a done link
  // because blur can be problematic
  input = $("<input type='text' size='10'>");
  input.insertBefore(a);
  input.blur(doneHandler);
  doneLink = $("<a href='#'>done</a>");
  doneLink.insertBefore(a);
  doneLink.click(doneHandler);

  // Put the text of the link in the input
  input.val(a.text());

  // Temporarily detach the link from the DOM to get it
  // out of the way
  a.detach();

  // Give the input focus, then wait for it to blur
  input[0].focus();

  // Our "done" handler
  function doneHandler() {
    // Replace the content of the original link with
    // the updated content
    a.text(input.val());

    // Put the link back, remove our input and other link
    a.insertBefore(input);
    input.remove();
    doneLink.remove();
  }
});

Live example 现场例子

That's not the only way you can do it, but it's fairly clear and simple. 这不是您唯一的方法,但它相当简单明了。 See the inline comments for description, and the docs (of course!) for details. 有关说明,请参见内联注释,有关详细信息,请参见文档 (当然!)。 Note that we use a closure for temporary storage of the detached link; 注意,我们使用闭包来临时存储分离的链接。 for more about closures, read Closures are not complicated . 有关闭包的更多信息,请阅读闭包并不复杂

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM