我想实现一个 VAT 切换器,如您在 http://aria.co.uk 上看到的那样右上角,尽管我只想在客户端执行此操作。
下面是我想出的东西,虽然我需要:
- 来回切换增值税(我的例子只是一种方式)
- 从 cookie 中保存和读取切换状态以保持持久性
让它不显眼/仅在 JS 可用时显示切换链接
<html>
<head>
<title>VAT Switch</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
function VATswitch(){
var price = $(".price strong span").text();
price = price * 10 / 12;
$(".price strong span").text(price);
var excl_incl = $(".price em").text();
excl_incl = "(excl. VAT)";
$(".price em").text(excl_incl);
}
</script>
</head>
<body>
<a href="#" onclick="VATswitch();" id="vat_switch">Switch VAT</a>
<p class="price">
<strong>£<span>120</span></strong>
<em>(incl. VAT)</em>
</p>
</body>
</html>
请帮忙。
最佳答案
最简单的方法是将两种价格呈现到您的页面,然后使用 jquery/css 控制可见性,例如:
<div class="price">
<span class="incVAT">£11.50 (incl VAT)</span>
<span class="exVAT">£10.00 (ex VAT)</span>
</div>
然后你的切换器可以做:
$('.price .incVAT').show();
$('.price .exVAT').hide();
反之亦然
编辑:我不会在客户端进行计算。假设您正在开一家商店,并不是所有的产品都需要缴纳增值税,有些可能有不同的税率。
编辑评论:
有一个jquery cookie library这将帮助你做 cookie,所以你需要做的就是在加载时读取值:
$(function(){
ShowPrices();
$('a#vattoggle').click(function(){
if($.cookie('VATMODE') == "INC"){
$.cookie('VATMODE', 'EX');
} else {
$.cookie('VATMODE', 'INC')
}
ShowPrices();
return false
});
});
function ShowPrices(){
if($.cookie('VATMODE') == "INC"){
$('.price .incVAT').show();
$('.price .exVAT').hide();
} else {
$('.price .incVAT').hide();
$('.price .exVAT').show();
}
}
关于javascript - JQuery 切换增值税切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/562815/