2016-01-01 21 views
0

大家好我想使用JQuery获取选定的行数据我能够获取可见字段的数据,但不能隐藏字段。这是我的WebGrid结合MVC5 jquery选择隐藏字段的行数据

@using (Html.BeginForm("DeleteSelected", "Email", FormMethod.Post)) 
{ 
    WebGrid grid = new WebGrid(Model); 
    @grid.GetHtml(
       tableStyle: "table", 
       selectedRowStyle: "selected", 
       headerStyle: "header", 
       alternatingRowStyle: "alternate", 
       htmlAttributes: new { id = "checkableGrid" }, 
       columns: grid.Columns 
       (
        grid.Column(
        format: @<text> <input class="checkbox-inline" type="checkbox" id="@item.MessageID" value="@item.MessageID" name="ids" /> </text>, 
        header: ""), 
        grid.Column("From", "From"), 
        grid.Column("Subject", "Subject"), 
        grid.Column("Body",format:@<text> <input type="hidden" id="@item.MessageID" name="IDHidden" value="@item.Body" /></text>), 
        grid.Column("MailDate", format: @<text> <input type="hidden" id="@item.MessageID" name="IDHidden" value="@item.MailDate" /></text>) 
       ) 
      ) 
    <span id="validationMessage" /> 
} 

我的JavaScript代码

$('#checkableGrid tr').each(function (i, e) { 
    $(e).children('tr td:not(:first)').css('cursor', 'pointer'); 
    $("#checkableGrid tr:nth-child(n) td").each(function() { 
    alert($(this).text()); 
`}); 
      $(e).children('td:not(:first)').click(function() { 
}) 
}) 

这样可以有一个人帮助我,我想读与隐藏字段每列数据。

<table class="table" id="checkableGrid"> 
<thead> 
     <tr class="header"> 
      <th scope="col"><input type="checkbox" id="cbSelectAll" value=""></th> 
      <th scope="col"> 
<a href="/Email/GetMails?sort=From&amp;sortdir=ASC">From</a>   </th> 
      <th scope="col"> 
<a href="/Email/GetMails?sort=Subject&amp;sortdir=ASC">Subject</a>   </th> 
      <th scope="col" style="display: none;"> 
<a href="/Email/GetMails?sort=Body&amp;sortdir=ASC">Body</a>   </th> 
      <th scope="col" style="display: none;"> 
<a href="/Email/GetMails?sort=MailDate&amp;sortdir=ASC">MailDate</a>   </th> 
     </tr> 
<tr> 
      <td> <input class="checkbox-inline" type="checkbox" id="3709" value="3709" name="ids"> </td> 
      <td style="cursor: pointer;">CouponDunia</td> 
      <td style="cursor: pointer;">New Year Offers &amp; More: Paytm Upto 222 Cashback, Amazon EOSS Upto70% Off, eBay Top Deals on Electronics &amp; More</td> 
      <td style="display: none; cursor: pointer;"> <input type="hidden" id="3709" name="IDHidden" value="some text" 
      <td style="display: none; cursor: pointer;"> <input type="hidden" id="3709" name="IDHidden" value="1/2/2016 1:34:56 AM"></td> 
     </tr> 
    </thead></table> 
+0

呈现的HTML看起来如何? – Shyju

回答

1

你的隐藏字段输入字段,text()方法不会that.You工作应该使用val()方法,让你输入字段的值。

$(function(){ 

    $('#checkableGrid tr').each(function (i, e) { 

    var _tr=$(this); 

     _tr.find("td").each(function(i,t) {  
     console.log($(this).text()); 
     }); 
     console.log("Will print input values now"); 

     _tr.find("input[type='hidden']").each(function(a,b) { 
     console.log($(this).val()); 
     });  

}); 


$(document).on("click","#checkableGrid td:not(:first)",function (e) { 
     console.log($(this).text()); 
}); 


}); 
+0

如何根据行点击获取数据 – demouser

+0

在我的回答中,我已将注册到td的点击事件记录到了td的文本中。你可以更新它来获得你想要的任何值。 – Shyju