使用jquery获取表中隐藏标签的值

Get value of hidden label in table using jquery?

本文关键字:标签 隐藏 jquery 获取 使用      更新时间:2023-09-26

我在列表视图中有一个html表,其中有一个隐藏的标签,名为"vehicle_num"。我可以获得表中所选的行以及任何td,但我似乎无法获得标签的值。我试过:

var vehicle_number = $(this).closest('tr').children('#vehicle_num').text();

下面是列表视图的代码。如何获取标签的值?

<asp:ListView ID="lvEquipmentList" runat="server" DataKeyNames="vehicle_number">
    <LayoutTemplate>
            <table id="table-equipment-list" class="table table-list">
                <thead>
                    <tr>
                        <th scope="col" class="product-line">Product line</th>
                        <th scope="col" class="model-number">Model #</th>
                        <th scope="col" class="serial-number">Serial #</th>
                        <th scope="col" class="dar-status">DAR Status</th>
                        <th scope="col" class="ship-date">Ship Date</th>
                    </tr>
                </thead>
                <tbody>
                    <asp:PlaceHolder ID="ItemPlaceholder" runat="server" /> 
                </tbody>
            </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <th scope="row" class="product-line"><div class="icon"><img src='<%#Eval("image_path")%>' onerror="this.src='assets/images/placeholder.png';" alt=""/></div> <span class="line-title"><%#Eval("product_line")%></span></th>
            <td class="model-number"><%#Eval("model")%><label class="vehicle_num" hidden="hidden"><%#Eval("vehicle_number")%></label></td>
            <td class="serial-number"><%#Eval("serial_number")%></td>
            <td class="dar-status"><img src='<%#Eval("display_status") %>'/></td>
            <td class="ship-date"><%#Eval("date")%></td>
        </tr>
    </ItemTemplate>
    <EmptyDataTemplate>
        <table id="table-equipment-list" class="table table-list">
            <thead>
                <tr>
                    <th scope="col" class="product-line">Product line</th>
                    <th scope="col" class="model-number">Model #</th>
                    <th scope="col" class="serial-number">Serial #</th>
                    <th scope="col" class="dar-status">DAR Status</th>
                    <th scope="col" class="ship-date">Ship Date</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <th scope="row" class="product-line"><div class="icon"></div> <span class="line-title"></span></th>
                    <td class="model-number"></td>
                    <td class="serial-number"></td>
                    <td class="dar-status"></td>
                    <td class="ship-date"></td>
                </tr>   
            </tbody>
        </table>
    </EmptyDataTemplate>
</asp:ListView>

编辑

我已经编辑了上面的代码,将标签放在表格列中。我能够使用获得值

var vehicle_number = $(this).closest('tr').find('.vehicle_num').text();

这是无效标记:

<tr>
    <th scope="row" class="product-line"><div class="icon"><img src='<%#Eval("image_path")%>' onerror="this.src='assets/images/placeholder.png';" alt=""/></div> <span class="line-title"><%#Eval("product_line")%></span></th>
    <td class="model-number"><%#Eval("model")%></td>
    <td class="serial-number"><%#Eval("serial_number")%></td>
    <td class="dar-status"><img src='<%#Eval("display_status") %>'/></td>
    <td class="ship-date"><%#Eval("date")%></td>
    <label id="vehicle_num" hidden="hidden" runat="server"><%#Eval("vehicle_number")%></label>
</tr>

有一个错误的label作为tr的子级,这是无效的。当浏览器试图理解这一点时,它可以使用label来构造一个有效的DOM,这将影响jQuery选择器。

label需要在表格单元格中:

<tr>
    <th scope="row" class="product-line"><div class="icon"><img src='<%#Eval("image_path")%>' onerror="this.src='assets/images/placeholder.png';" alt=""/></div> <span class="line-title"><%#Eval("product_line")%></span></th>
    <td class="model-number"><%#Eval("model")%></td>
    <td class="serial-number"><%#Eval("serial_number")%></td>
    <td class="dar-status"><img src='<%#Eval("display_status") %>'/></td>
    <td class="ship-date"><%#Eval("date")%></td>
    <td><label id="vehicle_num" hidden="hidden" runat="server"><%#Eval("vehicle_number")%></label></td>
</tr>

(或者,如果它总是隐藏的,你可以把它放在一个现有的表格单元格中。无论哪种方式都应该有效。)

此外,如果在此显式设置客户端id,则这些记录的任何重复都将导致DOM中出现重复的id,这也是无效的。当存在重复的id时,不同的浏览器以不同的方式响应基于id的选择器,因为行为是未定义的。你可能想用一个类来代替:

<label class="vehicle_num" ...

由于label不再是tr子级,而是其的子级

$(this).closest('tr').find('.vehicle_num')