简体   繁体   中英

Getting values from td using .closest is not giving out value

I create the table like so:

<table class="table" id="tbl_items">
<thead>
    <tr>
        <th>
            Id
        </th>
        <th>
            Item
        </th>
        <th>
            Serial Key
        </th>
        <th>
            Brand
        </th>
        <th>
            Quantity
        </th>
        <th>
            Description
        </th>
        <th>Action</th>
    </tr>
</thead>
<tbody>
    @foreach (var item in Model)
    {
        ViewBag.AccountID = item.AccountId.ToString();
        if (item.Items != null)
        {
            foreach (var itemOnList in item.Items)
            {
                <tr>
                    <td class="cls-itemid" data-itemid="@itemOnList.ItemId">@Html.DisplayFor(model => itemOnList.ItemId)</td>
                    <td class="cls-itemname" data-itemname="@itemOnList.ItemName">@Html.EditorFor(model => itemOnList.ItemName)</td>
                    <td class="cls-serialnumber" data-serialnumber="@itemOnList.SerialNumber">@Html.EditorFor(model => itemOnList.SerialNumber)</td>
                    <td class="cls-brandname" data-brandname="@itemOnList.BrandName">@Html.EditorFor(model => itemOnList.BrandName)</td>
                    <td class="cls-quantity" data-quantity="@itemOnList.Quantity">@Html.EditorFor(model => itemOnList.Quantity)</td>
                    <td class="cls-description" data-description="@itemOnList.Description">@Html.EditorFor(model => itemOnList.Description)</td>
                    <td>
                        <a href="javascript:void(0);" data-itemid="@itemOnList.ItemId" class="btn_edit">Edit |</a>
                        <a href="javascript:void(0);" data-itemid="@itemOnList.ItemId" class="btn_delete">Delete</a>
                    </td>
                </tr>
            }
        }
    }
</tbody>

I am trying to get the value of td in a row using this:

$(".btn_edit").click(function() {
    var row = $(this).closest("tr");
    var text = row.find(".cls-itemname").text();
    alert(text);
});

The alert box has no value. I cannot use the data-item because when I change the value of the @Html.EditorFor boxes, it gives the old values and not the new ones.

Since you use @Html.DisplayFor() which returns an <input> element... I guess you want to have the input's text...

This will work:

$(".btn_edit").click(function() {
  var row = $(this).closest("tr");
  var text = row.find(".cls-itemname input").val();  // <-- Look the change here
  alert(text);
});

There is simply no text in the .cls-itemname element... But there's an input having a value . Use .val() against the input .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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