有沒有辦法實現這一目標,但方法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