首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >隐藏(切换可见性)页面之间的元素

隐藏(切换可见性)页面之间的元素
EN

Stack Overflow用户
提问于 2013-12-17 13:36:15
回答 2查看 268关注 0票数 3

我在努力实现一些我不确定是可行的事情。我想要一个切换按钮来隐藏/显示页面上的元素。用jQuery很容易做到这一点。问题是,我希望在我打开的下一页上记住这个设置。

我有20页显示一个网站的价格。然后,切换按钮应该隐藏/显示页面上的价格。而且我不想每次打开新的页面时都要切换,所以如果我设置切换按钮来隐藏,那么价格应该隐藏在所有页面上,直到我再次按下按钮为止。

有谁知道这件事是怎么容易做到的?这是一个ASP页面,所以设置会话变量是一种解决方案,但是使用jQuery会更酷,因为元素是立即隐藏的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-17 16:01:43

实现这一目标的方法没有尽头,但是Viridis建议的方法听起来很可能是最好的方法。

  • 从ASP会话变量初始化价格的可见性(默认为可见)
  • 使用按钮的jQuery onClick显示/隐藏价格
  • 通过一个简单的ASP页面更新ASP会话变量,这个页面是通过同一buttom的AJAX onClick调用的。

这将是一个简化的(和未经测试的!)asp页面的版本:

代码语言:javascript
复制
<html>
<head>     
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" lang="javascript" type="text/javascript"></script>
    <script lang="javascript" type="text/javascript">

        //page variable storing the show / hide flag, initialised from the session variable
        <% if Session("hidePrices") = "Y" then %>
            var hidePrices = true;
        <% else %>
            var hidePrices = false;
        <%end if%>

        //worker to actually show / hide the prices
        function showHidePrices(pHidePrices)
        {
            if(pHidePrices)
                $(".myPrices").hide();
            else
                $(".myPrices").show();
        }

        //show / hide prices button click handler
        function showHidePricesOnClick()
        {
            //toggle the flag
            hidePrices = !hidePrices;

            //show / hide the prices
            showHidePrices(hidePrices);

            //toggle the flag stored in the session variable
            $.ajax({
                url: "http://www.yoursite.com/showHidePrices.asp",
                cache: false,
                success:function(result,status,xhr){
                     alert("Called showHidePrices.asp OK!");
                }, 
                error:function(xhr,status,error){
                     alert(xhr.responseText);
                     alert(status);
                     alert(error);
                }                 
            });
        }

        //hide the prices onload if necessary
        $( document ).ready(function() {

            if(hidePrices)
            {
                showHidePrices(true);
            }
        });

    </script>   

</head>
<body>

    <p class="myPrices">price 1</p>
    <p class="myPrices">price 2</p>
    <p class="myPrices">price 3</p>

    <input type="button" onclick="showHidePricesOnClick();" value="Show / Hide Prices"/>

</body>
</html>

showHidePrices.asp页面中的代码通过showHidePricesOnClick方法中的AJAX调用,如下所示:

代码语言:javascript
复制
<%
    if Session("hidePrices") = "Y" then
        Session("hidePrices") = "N"
    else
        Session("hidePrices") = "Y"
    end if
%>

正如我说过的,这是未经测试的(我确信这可以做得更优雅),但希望能帮助您大致了解您需要做什么才能实现您所追求的目标。

票数 2
EN

Stack Overflow用户

发布于 2013-12-17 13:47:12

您希望使用jQuery进行第一个切换,如何描述它。然后,您希望jQuery/js连接到后端(我假设是ASP),并为会话中的当前用户设置一些值。这样,jQuery/javascript就可以顺利地完成初始隐藏,然后如果您加载了一个新页面,那么后端就不会发送价格,从而导致这些价格根本不出现。(这样,您甚至不必再考虑使用jQuery删除它们了)

你必须编辑你所有的20页突然支持'dont_show_price‘参数.但它会得到你想要的结果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20635631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档