简体   繁体   English

使用jquery,如何获取表的选定行中@ Html.Hidden字段的值?

[英]Using jquery, how do I obtain the value of a @Html.Hidden field in a selected row of a table?

Given the following table, I know how to get the value of the first , but how can I use jquery to obtain the value of @Html.Hidden("AccountId")? 给定下表,我知道如何获取first的值,但是如何使用jquery获取@ Html.Hidden(“ AccountId”)的值? Assume that a certain row is already selected, ala: 假设已经选择了某一行,例如:

<script type="text/javascript>
        $("#tblTaskList").delegate("tr", "click", function () {
            $(this).addClass("selected").siblings().removeClass("selected");            
            var tids = $(this).find('td').first();
        });
</script>

and here is the table: 这是表格:

<table id="tblTaskList"> 
    <thead>
        <tr>
            <th>Ticket ID</th>                
        </tr>
    </thead>
    <tbody id="tblBody" class="tblBody">        
        @{for (var ix = 0; ix < Model.Value.Count - 1; ix++)
          { 
            <tr id="@(Model.Value[ix].TicketId)">
                <td style="width: 5%">@Model.Value[ix].TicketId</td>
                @Html.Hidden("AccountId");
            </tr>        
          }}
    </tbody>  
<table>

Thanks so much for the help 非常感谢你的帮助

The @Html.Hidden method renders an input element of type hidden - You can use the attribute equals selector : @Html.Hidden方法呈现hidden类型的input元素-您可以使用属性equals选择器

var hidden = $(this).find('input[type=hidden]').first();

eg: 例如:

$("#tblTaskList").delegate("tr", "click", function () {
    var self = $(this);
    self.addClass("selected").siblings().removeClass("selected");            
    var tids = self.find('td').first();
    var hidden = self.find('input[type=hidden]').first();
    ...
});

Obviously you don't want the hidden value to be alert()'d, but didn't know what you wanted to do with it at that point. 显然,您不希望将隐藏的值作为alert()的对象,但是不知道此时您想如何处理。 This will give you the val() of the hidden field within that . 这将为您提供其中的隐藏字段的val()。 Hope this helps! 希望这可以帮助!

<script type="text/javascript>
    $("#tblTaskList").on("click", "tr", function () {
        $(this).addClass("selected").siblings().removeClass("selected");            
        var tids = $(this).find('td').first();
        alert($(this).find('input:hidden').val());
    });
</script>

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

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