有没有办法实现这一目标,但方法Viridis建议听起来很可能是最好的方法。
- 初始化价格的知名度从ASP会话变量(默认为可见)
- 显示/使用按钮
- 更新通过一个简单的ASP页面的ASP会话变量的jQuery的onClick隐藏的价格,通过相同BUTTOM的AJAX的onClick叫
这将是一个简化(和未经考验的!)版本的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页面,通过AJAX在showHidePricesOnClick
方法调用的代码,会是这样的:
<%
if Session("hidePrices") = "Y" then
Session("hidePrices") = "N"
else
Session("hidePrices") = "Y"
end if
%>
正如我所说的,这是未经测试(并且可以更优雅的完成我当然可以),但希望能帮助你粗略地了解你需要做些什么来实现你的目标。
来源
2013-12-17 16:01:43
Ted
谢谢泰德发布一个完整的页面!非常感谢!我了解该页面应该如何工作,但看起来Ajax调用不起作用。当我在浏览器中运行showHidePrices.asp时,会话变量被切换,但当点击主页面上的按钮(上面的页面)时,不会发生。任何想法可能是错误的?我编辑了下面一行,没有别的:url:“showHidePrices.asp”。 – John
我在AJAX调用中添加了一些额外的错误处理,希望能为您提供一些关于失败原因的额外信息(例如找不到页面......)。关于jQuery AJAX调用的完整细节在这里:http://api.jquery.com/jQuery.ajax/ – Ted
我实际上第一次测试它(有点迟了,对不起!),并添加了一些有用的像jQuery的引用,在showHidePricesOnClick调用按钮之后的一些括号,最后,在AJAX调用中设置showHidePrices.asp的正确路径,一切都很好。希望你现在也能做到吗? – Ted