简体   繁体   English

使用jQuery将表格行中的特定tds放入编辑中(然后使用ajax更新)

[英]Put specific tds from a table row into edit using jQuery (then update w/ ajax)

I'm somewhat new to jQuery, so I could use some help here. 我是jQuery的新手,因此可以在这里使用一些帮助。

This is my issue: 这是我的问题:

I have a PHP script outputting a dynamic table. 我有一个输出动态表的PHP脚本。 Each row has an "edit" button, plus some other fields. 每行都有一个“编辑”按钮,以及其他一些字段。 Only 3 of those need to be turned into an input box. 其中只有3个需要变成输入框。 The edit button should only put that specific row into "edit mode." 编辑按钮应仅将特定行置于“编辑模式”。 I got as far as assigning each row a unique class by adding a number to the end of it. 我可以通过在每行的末尾添加一个数字来为每行分配一个唯一的类。

I have been able to use jQuery to change all of the rows into edit mode, but I need it to be specific to a row. 我已经能够使用jQuery将所有行更改为编辑模式,但是我需要特定于一行。

An example row would have classes like name0, price0, and desc0. 示例行将具有诸如name0,price0和desc0之类的类。 The next row would go on to classes name1, price1, and desc1 (for the fields that need changed). 下一行将转到类name1,price1和desc1(用于需要更改的字段)。 How can I reference these values and pass them to jQuery so it processes an event on just those elements? 如何引用这些值并将它们传递给jQuery,以便它仅对那些元素处理事件?

There are two ways of doing this: 有两种方法可以做到这一点:

  1. Dynamically creating the elements when the button is pressed; 按下按钮时动态创建元素; or 要么

  2. Hiding and showing elements that already exist. 隐藏和显示已经存在的元素。

Too much DOM manipulation can be really slow (particularly on certain browsers) so I favour (2). 太多的DOM操作可能真的很慢(尤其是在某些浏览器上),因此我赞成(2)。 So for example: 因此,例如:

<table class="editable">
<tbody>
<tr>
  <td>one</td>
  <td>
    <div class="view">two</div>
    <div class="edit"><input type="text"></div>
  </td>
  <td>
    <div class="view">three</div>
    <div class="edit"><input type="text"></div>
  </td>
  <td>
    <input type="button" class="edit" value="Edit">
    <input type="button" class="send" value="Send" disabled>
    <input type="button" class="cancel" value="Cancel" disabled>
  </td>
</tr>
</tbody>
</table>

with: 与:

table.editable div.edit { display: none; }

and

$(function() {
  $(":button.edit").click(function() {
    var row = $(this).closest("tr");
    row.find("input.view").attr("disabled", true");
    row.find("div.view").each(function() {
      // seed input's value
      $(this).next("div.edit").children("input").val($(this).text());
    }).fadeOut(function() { // fade out view
      row.find("div.edit").fadeIn(function() { // fade in edit
        row.find("input.edit").removeAttr("disabled"); // enable edit controls
      });
    });
  });
  $(":button.cancel").click(function() {
    var row = $(this).closest("tr");
    row.find("input.edit").attr("disabled", true");
    row.find("div.edit").fadeOut(function() {
      row.find("div.view").fadeIn(function() {
        row.find("input.view").removeAttr("disabled");
      });
    });
  });
  $(":button.save").click(function() {
    // ...
  });
});

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

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