Javascript 在单击表中最后一列的按钮时获取表的第一列值
我有一个视图,其中有一个包含四列和多行的表。三列有字符串值,最后一列有按钮。我的要求是,单击按钮时,我应该得到第一列的值。 我的代码如下:Javascript 在单击表中最后一列的按钮时获取表的第一列值,javascript,jquery,jquery-ui,Javascript,Jquery,Jquery Ui,我有一个视图,其中有一个包含四列和多行的表。三列有字符串值,最后一列有按钮。我的要求是,单击按钮时,我应该得到第一列的值。 我的代码如下: @(Html.Kendo().Grid<Invoice.Models.ViewModels.Setup.AccountViewModel>() .Name("Grid") .Columns(columns => { columns.Bound(p => p.AccountType).Title
@(Html.Kendo().Grid<Invoice.Models.ViewModels.Setup.AccountViewModel>()
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.AccountType).Title("Account Type").Width(200);
columns.Bound(p => p.AccountName).Title("Account Name").Width(200);
columns.Bound(p => p.Currency).Title("Currency").Width(200);
columns.Bound(p => p.lkpStatus).Width(225).HtmlAttributes(new { style = "text-align:center" })
.ClientTemplate("# if(data.lkpStatus == 1) " +
"{# <span class='label label-success'>Active</span> #} " +
"else if(data.lkpStatus ==2) {# <span class='label label-warning'>Blocked</span>#}"+
"else {#<span class='label label-important'>InActive</span># } #").Title("Status");
columns.Command(command => command.Edit()).ClientFooterTemplate("stst").Width(200).Title("Action");
}).ToolBar(toolbar =>
{
toolbar.Template(@<text>
<div class="row-fluid">
<div class="span1">
<div class="toolbar" style="height:25px;">
<ul id="menu" style="width:38px; margin-left:22px;" class="span6">
<li style="width:36px;">
<i class="fa fa-plus"></i>
<ul>
@foreach (var account in @ViewBag.AccountType)
{
<li style="width:100px;" class="openid">
@if (@account.Value == "Bank")
{
<label id="Bank1">@account.Value</label>
}
@if (@account.Value == "CreditCard")
{
<label id="Credit">@account.Value</label>
}
@if (@account.Value == "Cash")
{
<label id="Cash1">@account.Value</label>
}
</li>
}
</ul>
</li>
</ul>
</div>
</div>
</div>
</text>);
})
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("UpdateAccount"))
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.HtmlAttributes(new { style = "height:550px;" })
.DataSource(dataSource => dataSource
.Ajax()
//.Events(events => events.Error("error_handler"))
.Model(model => model.Id(p => p.AccountID))
.Read(read => read.Action("Account_Read", "Setup"))
.Update(update => update.Action("Account_Update", "Setup"))
)
) <script>
$('button').click(function() {
$(this).parent().siblings().each(function() {
alert('index ' + this.cellIndex + ': ' + $(this).text());
});
});
</script>
最简单的解决方案是,将:第一个选择器添加到同级
试试这个:
$('button').click(function() {
$(this).closest('tr').find('td').first().each(function() {
alert('index ' + this.cellIndex + ': ' + $(this).text());
});
});
这个怎么样
$('button').click(function() {
var first_text = $(this).closest('tr').children('td').first().text();
alert(first_text);
});
使用.closest函数将获得tr父项的第一个匹配项,然后使用.children.first查找第一个td子项。显示未定义的索引无法重现错误。我得到0。你试过JSFIDLE吗?我需要解决这个问题的方法。在给出第一个答案后,请不要完全改变你的问题。
$('button').click(function() {
$(this).closest('tr').find('td').first().each(function() {
alert('index ' + this.cellIndex + ': ' + $(this).text());
});
});
$('button').click(function() {
var first_text = $(this).closest('tr').children('td').first().text();
alert(first_text);
});