jQuery 如何使用jquery获取表行的属性值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12510593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:46:42  来源:igfitidea点击:

how to get the attribute values of the table row using jquery?

jqueryattributes

提问by Sravan

I have a following razor to create a table.

我有一个以下剃刀来创建一个表格。

@model IEnumerable<iBoxV5.Model.Common.AdvancedSearch>
<table id="SearchFieldsTable">
  <thead>
    <tr>
    <th>
        ColumnName
    </th>
    <th>
        FilterValues
    </th>
    <th>Updated Before</th>
    </tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr [email protected] [email protected] [email protected] [email protected] id=@(index++)>
    <td>
        @Html.DisplayFor(modelItem => item.ColumnName)
    </td>
    <td>
        @Html.TextBoxFor(modelItem => item.FilterValue, new { @class = "FrmTextBox advSearch" });

        @Html.TextBoxFor(modelItem => item.FilterValue, new { @class = "FrmTextBox advSearch" });
    </td>
    <td>
        @Html.TextBoxFor(modelItem => item.FilterValueTo, new { @class = "FrmTextBox advSearch" });
    </td>
</tr>
}
</tbody>
</table>

And In the above I have added few attributes for the like ColumnName.

在上面,我为 ColumnName 之类的添加了一些属性。

I wants to build a JSON for the each row of the Table.

我想为表的每一行构建一个 JSON。

I tried with the following jquery Snippet.

我尝试使用以下 jquery 片段。

var SearchFieldsTable = $("#SearchFieldsTable tbody");

var trows = SearchFieldsTable[0].rows;

$.each(trows, function (index, row) {
    var ColumnName=$(row).attr("ColumnName");
});

But the above is not returning the ColumnName I expected.

但是上面没有返回我期望的 ColumnName 。

回答by Muru Bakthavachalam

try this;

尝试这个;

$("#trowId").attr("attrbuteName");

回答by Bharath

May be this will be helpful.

可能这会有所帮助。

var SearchFieldsTable = $("#SearchFieldsTable tbody");

var trows = SearchFieldsTable.children("tr");

$.each(trows, function (index, row) {
    var ColumnName=$(row).attr("ColumnName");
    // .... Your codes here 
});

FYI:"tbody" don't have "rows". The following is wrong

仅供参考:“tbody”没有“行”。以下是错误的

SearchFieldsTable[0].rows

You can use the following code instead to use above code.

您可以使用下面的代码来代替上面的代码。

var SearchFieldsTable = $("#SearchFieldsTable");

回答by NullPoiиteя

you can get the value by either class or id like

您可以通过 class 或 id 获取值,例如

var ColumnName=$(row).attr(".ColumnName").val();

or

或者

var ColumnName=$(row).attr("#ColumnName").val();

回答by undefined

You are defining a local variable in each iteration, now your ColumnName variable only stores the attribute of last row. Also ColumnNamein not a valid attribute. You can use data-*attributes and mapmethod.

您在每次迭代中定义一个局部变量,现在您的 ColumnName 变量仅存储最后一行的属性。也是ColumnName无效的属性。您可以使用data-*属性和map方法。

var columnName = $("#SearchFieldsTable tbody tr").map(function(){
                      return $(this).data('ColumnName')
                 })

Now columnName is an array of column names.

现在 columnName 是一个列名数组。

回答by Praveen Kumar Purushothaman

Some browsers won't support custom attributes. So, you can better use data().

某些浏览器不支持自定义属性。因此,您可以更好地使用data().

var ColumnName = $(row).data("ColumnName");

And in the row, define it as:

在行中,将其定义为:

<tr [email protected] ...