简体   繁体   English

如何使用jQuery和JavaScript选择一行中的特定列?

[英]How can I select a specific column in a row using jQuery and JavaScript?

I am very new to jQuery and JavaScript. 我是jQuery和JavaScript的新手。 I have a small question. 我有一个小问题。 Let's say i have a HTML table like the following 假设我有一个如下所示的HTML表格

<Table id="mytable">
 <tr id="element">
  <td>value</td>
  <td>text</td>
</tr>
</Table>

In the above example i know the row id and i want to change the value of the second column of the row with that particular id. 在上面的例子中,我知道行id,我想更改具有该特定id的行的第二列的值。

I need a result something like the following: 我需要一个如下结果:

 <Table id="mytable">
 <tr id="element">
  <td>value</td>
  <td>ChangedText</td>
</tr>
</Table>

So my question is: how can I select the 2 nd column of the first row with a given id in order to change the value? 所以我的问题是:我如何选择具有给定id的第一行的第二列才能更改值?

$("#element td:nth-child(2)").text('ChangedText');

这是一个例子

something like 就像是

$('#mytable tr:eq(0) td:eq(1)').text('ChangedText');

will select the first row, second column (0 based) of the given element (TABLE). 将选择给定元素(TABLE)的第一行,第二列(基于0)。 In your case, since you know the row id : 在您的情况下,因为您知道行ID:

$('#mytable #element td:eq(1)').text('ChangedText');

or simply 或者干脆

$('#element td:eq(1)').text('ChangedText');

Gert's code is how I would have implemented what you are asking so I won't repost it. Gert的代码是我如何实现你所要求的,所以我不会重新发布它。 However since you are new to jquery/javascript, you might like this tool I use to make sure my selectors are working http://www.woods.iki.fi/interactive-jquery-tester.html . 但是,由于您不熟悉jquery / javascript,您可能会喜欢这个工具,以确保我的选择器正在工作http://www.woods.iki.fi/interactive-jquery-tester.html

Cheers, Joe 干杯,乔

暂无
暂无

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

相关问题 如何识别特定项目 <select>并在jquery中的每一行添加一个删除按钮 - How can I identify a specific item in <select> and add a delete button each row in jquery 我可以使用jquery或javascript将数据连续添加到特定的td吗? - Can I use jquery or javascript to append data to a specific td in a row? 如何使用jQuery从每一行中随机选择一列? - How can I use jQuery to randomly select one column from each row? 每当用户使用jQuery在选择下拉列表中选择特定值时,如何执行功能? - How can I perform a function each time that the user select a specific value into a select dropdown using jQuery? 使用Javascript或JQuery从特定的表行和列中获取选择值和选项 - Get Select Value and Option from Specific Table Row and Column with Javascript or JQuery 如何使用JavaScript在多维数组中查找特定元素的行和列 - How to find the row and column of a specific element in a multidimensional array using javascript 如何使用javascript获取要单击的表行特定列? - How to get table row specific column to be clicked using javascript? 如何在javascript / jquery中的特定符号后选择字符串? - How do I select string after a specific symbol in javascript/Jquery? 使用 JavaScript:当我编辑特定的 html 表格行时使用模态类。 如何在下拉列表中选择当前值? - Using JavaScript: Using a modal class when i am editing a specific html table row. how to select the current value on a dropdownlist? 如何使用JavaScript确保每个表行中都有特定的字符串? - How can I make sure a specific string exists in every table row using JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM