将输入的值转换或提取为纯文本

时间:2022-01-27 19:36:56

I've got this js function that converts ALL input values into text. I want to convert only ONE row from to plain text (all rows have 5 cells plus a button for saving) and with that button i want to convert that row data into plain text. I was trying to use .closest object but i failed.

我有这个js函数,它将所有输入值转换为文本。我只需要将一行从纯文本转换为纯文本(所有行都有5个单元格和一个用于保存的按钮),并使用这个按钮将行数据转换为纯文本。我试着用。最近的对象,但是失败了。

 function disable_inp() {
    $("tr:gt(0) td:has(input)").text(function() {
      return $('input', this).val();
    });
  }

2 个解决方案

#1


1  

You should use :eq and not :gt. The eq gets the index of the selection, while gt is greater than.

你应该使用:eq,而不是:gt。eq得到选择的指标,gt大于。

function disable_inp() {
  $("tr:eq(0) td:has(input)").text(function() {
    return $('input', this).val();
  });
}

And with the button, you can do the same:

有了按钮,你也可以这样做:

$("button").click(function disable_inp() {
  $(this).closest("tr").find("td:has(input)").text(function() {
    return $('input', this).val();
  });
});

Here, the .closest() will find the first parent that matches the selection and applies from there.

在这里,. closer()将找到匹配所选内容并从那里应用的第一个父元素。

#2


0  

button.click(function(){
    var $row = $(this).parent().parent(); //assuming it's not wrapped this will be the <tr>
    $row.find("td").each(function(){
        var plaintext = $(this).text();
        // do stuff with the plaintext
    });
});

#1


1  

You should use :eq and not :gt. The eq gets the index of the selection, while gt is greater than.

你应该使用:eq,而不是:gt。eq得到选择的指标,gt大于。

function disable_inp() {
  $("tr:eq(0) td:has(input)").text(function() {
    return $('input', this).val();
  });
}

And with the button, you can do the same:

有了按钮,你也可以这样做:

$("button").click(function disable_inp() {
  $(this).closest("tr").find("td:has(input)").text(function() {
    return $('input', this).val();
  });
});

Here, the .closest() will find the first parent that matches the selection and applies from there.

在这里,. closer()将找到匹配所选内容并从那里应用的第一个父元素。

#2


0  

button.click(function(){
    var $row = $(this).parent().parent(); //assuming it's not wrapped this will be the <tr>
    $row.find("td").each(function(){
        var plaintext = $(this).text();
        // do stuff with the plaintext
    });
});