【问题标题】:Why the element shows again when I hide it using jquery为什么当我使用 jquery 隐藏它时元素再次显示
【发布时间】:2015-12-15 11:14:26
【问题描述】:

我在网页中有一个列表视图控件,在列表视图中,我有一个链接按钮。我在 updatepanel 控件中的 LinkBut​​ton。当我单击 LinkBut​​ton 时,计数器字段 +1。我使用 ClientIDMode="AutoID" 因为我的页面没有完整的 postpack。现在,我希望在单击 LinkBut​​ton 时,LinkBut​​ton 的显示等于“无”。为此,我使用以下脚本:

<script type="text/javascript">
function updateTextArea() {
    $('.like').on('click', 'a', function () {
        $(this).hide();
    });
}

<script type="text/javascript">
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(updateTextArea);
</script>

现在,我的问题是:当我单击 LinkBut​​ton 时,LinkBut​​ton 被隐藏并再次显示。我的错在哪里。请帮我。非常感谢。

这是我的 aspx 代码:

<asp:ListView ID="PortfolioListView" runat="server" onitemcommand="PortfolioListView_ItemCommand">
                <ItemTemplate>
                    <li class="item brick1 <%# Eval("CategoryName")%> isotope-item">
                        <a class="item-popup" href="Gallery/195x195/<%# Eval("MainImage") %>" title="<%# Eval("ShortDesc") %>">
                            <img src="Gallery/195x195/<%# Eval("MainImage") %>" alt="<%# Eval("Title") %>" />
                            <div class="hover">
                                <span class="Popup"><i class="fa fa-search-plus"></i></span>
                                <span><%# Eval("CategoryName")%></span>
                            </div>
                        </a>
                        <div class="bottom">
                            <div class="title"><span><%# Eval("Title")%></span></div>
                            <asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate>
                            <div class="like"><asp:LinkButton ID="LikeLBTN" runat="server" ClientIDMode="AutoID" OnClientClick="updateTextArea()" CommandName="Like" CommandArgument="<%# Bind('GalleryID') %>"><i class="fa fa-thumbs-o-up"></i><span><%# Eval("Counter")%></span></asp:LinkButton></div>
                            </ContentTemplate></asp:UpdatePanel>
                        </div>
                    </li>
                </ItemTemplate>
            </asp:ListView>

【问题讨论】:

  • 当您检查元素并查看时究竟会发生什么? display: none; 是否存在或被覆盖或其他什么?
  • 当我点击 LinkBut​​ton 时,display:none 添加到标签,然后它将被删除。
  • it will be removed - 为什么?谁?在哪里?
  • 在 OnClientClick 中添加 return false 以避免回发 &lt;div class="like"&gt;&lt;asp:LinkButton ID="LikeLBTN" runat="server" ClientIDMode="AutoID" OnClientClick="updateTextArea();return false;" CommandName="Like" CommandArgument="&lt;%# Bind('GalleryID') %&gt;"&gt;&lt;i class="fa fa-thumbs-o-up"&gt;&lt;/i&gt;&lt;span&gt;&lt;%# Eval("Counter")%&gt;&lt;/span&gt;&lt;/asp:LinkButton&gt;&lt;/div&gt;

标签: jquery asp.net listview updatepanel linkbutton


【解决方案1】:

这是由于 asp.net 控件的自动发布功能而发生的。更新您的 java-script 函数,其余代码将相同。

function updateTextArea() {
        $('.like').on('click', 'a', function () {
            $(this).hide();
            return false;
        });
    }

【讨论】:

  • 返回 false 时,LinkBut​​ton 被隐藏,不再显示。很好,但是当我单击链接按钮时,是否应该将一个添加到喜欢的数量(计数器字段)。像保存在数据库中并在列表视图中使用 CommandName="Like" 的数量,我更新了这个字段。当我在脚本中使用 return false 时,后面代码中的更新命令不起作用。还有其他解决方案吗? Tnx。
  • 如果是这种情况,那么您可以使用 aspx.cs 文件来处理隐藏或其他事件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-19
  • 2014-05-16
  • 1970-01-01
  • 1970-01-01
  • 2022-10-04
相关资源
最近更新 更多