简体   繁体   English

如何使用JQuery从表中选择第一行

[英]How to select 1st row from table using JQuery

I want to select first row from table data when page first loads. 我想在页面首次加载时从表数据中选择第一行。 My $('#Home').on('click', '#tblStyles .HomeSelect', function () { does not hit. Please suggest me here I am going wrong. 我的$('#Home').on('click', '#tblStyles .HomeSelect', function () {未命中。请在这里建议我我做错了。

Report View 报告检视

<ul class="nav nav-tabs">
            <li class="active"><a data-toggle="tab" href="#Home">Home</a></li>          
        </ul>
        <div class="tab-content">
            <div id="Home" class="tab-pane fade in active">
                @Html.Partial("~/Views/Home/Home.cshtml", Model.clsHome)
            </div>

        </div>

Home View 主视图

<div class="panel panel-default">
        <table id="tblStyles" class="table table-hover">
            <tr class="no-hover" style="border-top:hidden">
                <th>
                    @Html.DisplayNameFor(m => m.ProductName)
                </th>            

            </tr>

            @foreach (var item in Model)
            {
                <tr class="HomeSelect" data-productcode="@item.ProductCode">

                    <td align="right">
                        @Html.DisplayFor(modelItem => item.ProductCode)
                    </td>

                </tr>
            }
        </table>
    </div>
            @using (Html.BeginForm("Home", "Home", FormMethod.Post, new { id = "Form" }))
        {
            <div id="mypartial"> </div>

        }

Scripts 剧本

   $(document).ready(function () {             
            $('#Home').find('#tblStyles tr:first').addClass("selected").click();              
        });

     $('#Home').on('click', '#tblStyles .HomeSelect', function () {

            var id = $(this).data('productcode');

Your jQuery selector $('#Home').find('#tblStyles tr:first') will give you the first table row. 您的jQuery选择器$('#Home').find('#tblStyles tr:first')将为您提供第一行表格。 That does not mean that it is the first table row with the CSS class HomeSelect . 这并不意味着它是CSS类HomeSelect的第一表行。 Infact your selector will give you the first row, which is the table header (Where you are calling the DisplayNameFor helper method. 实际上,选择器将为您提供第一行,即表标题(在其中调用DisplayNameFor helper方法的位置)。

Your click event handler is wired up for elements with HomeSelect CSS Class. 您的click事件处理程序已连接到带有HomeSelect CSS类的元素。 So make sure when you invoke the first tr's click event, you follow the same pattern. 因此,请确保在调用第一个tr的click事件时,遵循相同的模式。

Make sure you are invoking the click event using JavaScript after wiring up the click event handler. 请确保您调用click连接最多的单击事件处理程序后,使用JavaScript事件。 I suggest you wire up any event handlers inside the jquery ready event. 我建议您在jquery ready事件中连接所有事件处理程序。

$(function () {
    $('#Home').on('click', '#tblStyles .HomeSelect', function () {
        var id = $(this).data('productcode');
        alert(id);
    });

    // Invoke click on the first row with "HomeSelect" CSS class
    // The header row does not have that class.
    $('#Home').find('#tblStyles tr.HomeSelect:first').click();  
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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