Javascript jQuery cookie破坏我的CSS切换输入
我使用jQuerycookies和jQueryHide-and-show以及复选框切换来控制何时显示价格Javascript jQuery cookie破坏我的CSS切换输入,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我使用jQuerycookies和jQueryHide-and-show以及复选框切换来控制何时显示价格 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script> <label class="switch"> <input type="checkbox" id="togBtn">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<label class="switch">
<input type="checkbox" id="togBtn">
<div class="slider round">
<span class="on">Incl</span>
<span class="off">Excl</span>
</div>
</label>
<span class="price-excluding-tax">
<span class="label">Excl. Tax:</span>
<span class="price" id="price-excluding-tax-3">€9.99</span>
</span>
<span class="price-including-tax">
<span class="label">Incl. Tax:</span>
<span class="price" id="price-including-tax-3">€9.99</span>
</span>
<script type="text/javascript">
(function($){
$(function(){
ShowPrices();
$('input#togBtn').click(function(){
if($.cookie('VATMODE') == "INC"){
$.cookie('VATMODE', 'EX');
} else {
$.cookie('VATMODE', 'INC')
}
ShowPrices();
return false
});
});
function ShowPrices(){
if($.cookie('VATMODE') == "INC"){
$('.price-including-tax').show();
$('.price-excluding-tax').hide();
} else {
$('.price-including-tax').hide();
$('.price-excluding-tax').show();
}
}
})(jQuery);
</script>
包括
不包括
不含税:
€9.99
含税:
€9.99
(函数($){
$(函数(){
ShowPrices();
$('input#togBtn')。单击(函数(){
如果($.cookie('VATMODE')==“INC”){
$.cookie('VATMODE','EX');
}否则{
$.cookie('VATMODE','INC')
}
ShowPrices();
返回错误
});
});
函数ShowPrices(){
如果($.cookie('VATMODE')==“INC”){
$('价格含税').show();
$('价格不含税').hide();
}否则{
$('.price include tax').hide();
$('价格不含税').show();
}
}
})(jQuery);
除了jQuery破坏了我的CSS切换之外,这一切都很正常,我一辈子都不知道为什么
这里是一个问题(注意开关如何不切换):
在我的切换功能正常的地方,jQuery也被删除了:
为什么返回带有
$('input#togBtn')
的false
?拆除返回装置
用
$('input#togBtn')
检查为什么返回false
?拆除返回装置
检查您的两个小提琴仍然有jQuery。只是其中一个没有包含cookies库。没有cookies库的是可以工作的,因为您的点击处理程序从未设置过 这是因为您的代码在
ShowPrices()
因为cookie库不在那里,$。cookie
没有定义,因此会抛出一个错误。如果打开控制台(F12->console选项卡,在大多数浏览器上),您会看到一些错误,如uncaughttypeerror:$。cookie不是函数
此错误会影响函数中的其余代码未运行
ShowPrices(); //error happens in ShowPrices, execution stops here does not continue
$('input#togBtn').click(function(){
//...
});
带有cookies库的一个无法工作的原因是,从未抛出错误,并且您的单击处理程序已设置。在该处理程序中,您返回false
,这将阻止复选框更改状态的默认操作
jQuery(“#thecheck”)。单击(函数(){return false})代码>
您的两个小提琴仍然有jQuery。只是其中一个没有包含cookies库。没有cookies库的是可以工作的,因为您的点击处理程序从未设置过
这是因为您的代码在ShowPrices()
因为cookie库不在那里,$。cookie
没有定义,因此会抛出一个错误。如果打开控制台(F12->console选项卡,在大多数浏览器上),您会看到一些错误,如uncaughttypeerror:$。cookie不是函数
此错误会影响函数中的其余代码未运行
ShowPrices(); //error happens in ShowPrices, execution stops here does not continue
$('input#togBtn').click(function(){
//...
});
带有cookies库的一个无法工作的原因是,从未抛出错误,并且您的单击处理程序已设置。在该处理程序中,您返回false
,这将阻止复选框更改状态的默认操作
jQuery(“#thecheck”)。单击(函数(){return false})代码>
谢谢,我知道这一定很愚蠢。。。与问题无关,但是关于如何使用cookies使复选框状态持续存在,有什么想法吗?谢谢,我知道这一定很愚蠢。。。与问题无关,但是关于如何使用cookies使复选框状态保持不变,您有什么想法吗?