[英]Get the text inside span tag of dynamically generated table?
I have a dynamically generated page of an enterprise application. 我有一个动态生成的企业应用程序页面。 The data is inside table structure. 数据在表结构内部。 I need to access a value of span tag inside this table. 我需要访问此表中的span标签的值。
<table class="table table-bordered table-hover table-striped">
<thead>
...
</thead>
<tbody>
@foreach (var item in Model.Select((x, i) => new { Data = x, Index = i }))
{
<tr>
<td>@(item.Index + ((Model.PageNumber - 1) * Model.PageSize) + 1)</td>
<td>
**<span class="site-name">
@Html.DisplayFor(modelItem => item.Data.SITENAME)
</span>** //Want to access item inside span
</td>
<td>@Html.DisplayFor(modelItem => item.Data.REMARKS)</td>
<td>
<a href="@Url.Action("Delete", new { id = item.Data.ID })"
class="btn btn-sm btn-danger delete-site">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</a>
</td>
</tr>
}
</tbody>
Jquery looks like this jQuery看起来像这样
$(document).on("click", ".delete-site", function (e) {
var item = $(this);
var name = $(item.closest("span[class='site-name']"));
alert(name.text());
return false;
});
Here i am getting an empty alertbox. 在这里,我得到一个空的警报框。
How can I get the value which is inside span tag using jquery?? 如何使用jquery获取span标记内的值?
Replace this: 替换为:
var name = $(item.closest("span[class='site-name']"));
With this: 有了这个:
var name = $(item.closest("tr").find("span[class='site-name']"));
Because .site-name
span isn't inside your parent td, so use closest to use nearest parent tr of having .site-name
and then use find to get the element 由于.site-name
span不在您的父代td内,因此,请使用最接近来使用具有.site-name
最近父代tr,然后使用find来获取元素
Try This 尝试这个
$(document).on("click", ".delete-site", function (e) {
var item = $(this);
var name = $(item).find("span[class='site-name']");
alert(name.text());
return false;
});
Try this 尝试这个
$(document).on("click", ".delete-site", function (e) {
var item = $(this);
var name = item.closest("span").find("site-name");
alert(name.text());
return false;
});
What you need is to find the closest <tr>
and then find the span 您需要找到最接近的<tr>
,然后找到跨度
$(document).on("click", ".delete-site", function(e) {
var item = $(this);
var name = item.closest("tr").find("td span[class='site-name']");
alert(name.text());
return false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.