简体   繁体   English

在表格中隐藏一行并再次显示?

[英]Hide a row in a table and display it again?

I use a foreach loop inside ASP.NET MVC View page. 我在ASP.NET MVC视图页面中使用了foreach循环。 For each element of the collection that foreach operates on I create two rows - one for display, one for edit. 对于foreach操作的集合中的每个元素,我创建两行-一列用于显示,一列用于编辑。 I want to hide the edit row and only display it later depending on user action. 我想隐藏编辑行,仅在以后根据用户操作显示它。

If I hide the edit rows with display: none , then jQuery's show() method cannot redisplay it again - it doesn't work. 如果我用display: none隐藏编辑行,则jQuery的show()方法无法再次重新显示它-它不起作用。 If I hide it like this 如果我这样藏起来

// I put this inside the foreach loop
<script type="text/javascript">
$("#edititem_" + <%: item.Id %>).hide();
</script>

jQuery's show() function can display it later but the page does not validate because this is inside <tbody> tag (this is where I enumerate my collection and create <tr> 's) jQuery的show()函数可以稍后显示它,但是页面无法验证,因为它位于<tbody>标记内(这是我枚举集合并创建<tr>的地方)

I want to be able to show/hide edit rows on demand and still have a XHTML valid page. 我希望能够按需显示/隐藏编辑行,并且仍然具有XHTML有效页面。

How can I achieve that? 我该如何实现?

To have graceful degredation as well, you could stick with the hiding done via jQuery, just give the edit <tr> rows a class, eg <tr class="edit"> then hide all of those at once with jQuery outside the table, like this: 要同时实现优雅的降级效果,您可以坚持通过jQuery进行隐藏,只需将<tr>编辑行提供一个类,例如<tr class="edit">然后使用jQuery将所有这些隐藏在表外,像这样:

<script type="text/javascript">
  $(function() {
    $("tr.edit").hide();
  });
</script>

This approach, instead of hiding them off the bat with CSS is more friendly to those with JS disabled, if you might have an audience of users like that, go with this .hide() approach. 对于那些禁用JS的用户来说,这种方法比使用CSS隐藏它们更容易,如果您有这样的用户群,则可以使用.hide()方法。

use a CSS file and add a Class to the edit-row and another class to the showing row. 使用CSS文件,然后在“编辑行”中添加一个“ Class ,并在显示的行中添加另一个“类”。

if you write 如果你写

.editRows {
  display:none;
}

in the CSS file, the .show() of jquery works. 在CSS文件中,jquery的.show()有效。

or you could do 或者你可以做

$(".editRows").hide();

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

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