我在努力实现一些我不确定是可行的事情。我想要一个切换按钮来隐藏/显示页面上的元素。用jQuery很容易做到这一点。问题是,我希望在我打开的下一页上记住这个设置。
我有20页显示一个网站的价格。然后,切换按钮应该隐藏/显示页面上的价格。而且我不想每次打开新的页面时都要切换,所以如果我设置切换按钮来隐藏,那么价格应该隐藏在所有页面上,直到我再次按下按钮为止。
有谁知道这件事是怎么容易做到的?这是一个ASP页面,所以设置会话变量是一种解决方案,但是使用jQuery会更酷,因为元素是立即隐藏的。
发布于 2013-12-17 16:01:43
实现这一目标的方法没有尽头,但是Viridis建议的方法听起来很可能是最好的方法。
这将是一个简化的(和未经测试的!)asp页面的版本:
<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调用,如下所示:
<%
if Session("hidePrices") = "Y" then
Session("hidePrices") = "N"
else
Session("hidePrices") = "Y"
end if
%>正如我说过的,这是未经测试的(我确信这可以做得更优雅),但希望能帮助您大致了解您需要做什么才能实现您所追求的目标。
发布于 2013-12-17 13:47:12
您希望使用jQuery进行第一个切换,如何描述它。然后,您希望jQuery/js连接到后端(我假设是ASP),并为会话中的当前用户设置一些值。这样,jQuery/javascript就可以顺利地完成初始隐藏,然后如果您加载了一个新页面,那么后端就不会发送价格,从而导致这些价格根本不出现。(这样,您甚至不必再考虑使用jQuery删除它们了)
你必须编辑你所有的20页突然支持'dont_show_price‘参数.但它会得到你想要的结果。
https://stackoverflow.com/questions/20635631
复制相似问题