简体   繁体   English

使用Jquery编辑HTML表数据单元格

[英]Edit HTML Table Data cell using Jquery

I have an HTML table, and each cell of the table will have two data attributes. 我有一个HTML表,该表的每个单元格都有两个数据属性。 What I'm trying to do is set a button to switch the value being shown in the table between those two attributes. 我想做的是设置一个按钮,以在表中显示的这两个属性之间切换值。

<table class="table1">
<tbody>
<tr>
<td data-original="A" data-new="B"> A </td>
</tr>
</tbody>
</table>

I'm able to set new text and get attributes outside the table, but whenever I try to within the table I keep receiving an error: 我可以在表外设置新文本并获取属性,但是每当尝试在表内时,我都会不断收到错误消息:

'Uncaught -> TypeError: undefined is not a function'. “未捕获-> TypeError:未定义不是函数”。

I've been receiving this error for a number of commands $('td').text() , .val() , .attr('td') , .getAttribute() . 我已经收到许多命令$('td').text() .val() .attr('td').getAttribute() Am I missing a plugin or something for getting and setting values from tables? 我是否缺少插件或从表中获取和设置值的东西?

ANSWER: I figured out the reason, I was an idiot and didn't mention that there would be numerous TD elements with repeating tags. 答案:我想出了原因,我是个白痴,没有提到会有很多带有重复标签的TD元素。 I eventually used Underscore.js's each method to iterate through them and parts of the below answer to swap the values. 最终,我最终使用Underscore.js的each方法来遍历它们以及下面的部分答案以交换值。

Just made a Fiddle : 刚刚做了一个小提琴

$("button").on("click", function () {
    $("td").text($.trim($("td").text()) == $("td").data("original") 
      ? $("td").data("new") : $("td").data("original"));
});

to switch between the data-original and data-new values by checking the current text in the td and using a ternary operator. 通过检查td中的当前文本并使用三元运算符在data-original值和data-new值之间切换。 By using trim() for the initial text issues in case of whitespace are taken care of (as I just noticed that you have whitespace in your example td). 通过使用trim()作为初始文本,可以解决空白问题(就像我刚才注意到的示例td中有空白)。

Just in case the button isn't already in the DOM when the page is initially loaded, you have to adjust the on() to delegate the click event from a static parent element to the button, eg like this: $(document).on("click", "button", function () { ... 以防万一最初加载页面时按钮尚未在DOM中,您必须调整on()才能将click事件从静态父元素委派给按钮,例如: $(document).on("click", "button", function () { ...
Instead of $(document) every other static parent element can be used. 可以使用每个其他静态父元素代替$(document)

And as you mentioned that the table will have multiple tds with data-attributes, I've just adjusted the Fiddle to take care of that: 正如您提到的,该表将具有多个带有数据属性的tds,我刚刚对Fiddle进行了调整以解决这一问题:

$("button").on("click", function () {
   $("td").each(function () {
    $(this).text($.trim($(this).text()) == $(this).data("original") ?   
             $(this).data("new") : $(this).data("original"));
   });
});

I don't know how .text() didn't work for you. 我不知道.text()对您不起作用。

To set text inside td elements, you use .text() . 要在td元素内设置文本,请使用.text() To get the data inside data-current or data-new , jQuery has a handy function .data(tag) , for example $(sel).data('current') . 为了获取数据为data-currentdata-new ,jQuery具有一个方便的函数.data(tag) ,例如$(sel).data('current')

Here's a fiddle displaying usage of this on your problem. 这是一个小提琴,用于显示您的问题的用法。

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

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