Javascript 如何在条件下检查两个变量?
如果我不检查供应商和客户,我想隐藏表格Javascript 如何在条件下检查两个变量?,javascript,jquery,Javascript,Jquery,如果我不检查供应商和客户,我想隐藏表格 非常感谢。当前您的if子句如下所示: $('.supplier').on('click', function(){ var supplier = $('.supplier').is(":checked"); var customer = $('.customer').is(":checked"); // if both is unchecked, hide the table if( sup
非常感谢。当前您的if子句如下所示:
$('.supplier').on('click', function(){
var supplier = $('.supplier').is(":checked");
var customer = $('.customer').is(":checked");
// if both is unchecked, hide the table
if( supplier && customer == false){
alert('hide table');
}
// if supplier is checked, show supplier, else hide supplier
});
您还需要将供应商
与错误
进行比较,以便使用:
if( supplier == true && customer == false){
alert('hide table');
}
或:
而不是:
if( !supplier && !customer){
alert('hide table');
}
在您的上一行中,供应商
是布尔变量。所以你可以用你想用的方式
var supplier = $('.supplier').is(":checked");
请尝试以下操作:
if( !supplier && !customer){
alert('hide table');
}
或
您可以使用betwise运算符
if (!supplier && !customer){
alert('hide table');
}
在前面的回答中,有一点没有真正提到(尽管答案很好),那就是您只在单击
.supplier
时调用函数。我可以想象,当您单击.supplier
或.customer
时,您想要调用此函数。如果是这样,您需要将.customer
添加到选择器中:
if( supplier & customer == false){
//alert
}
您必须检查这两个条件使用此
supplier==false&&customer==false
而不是supplier&&customer==false
。非常感谢兄弟,我忘记插入了。客户:D
if (supplier == false && customer == false){
alert('hide table');
}
if (!supplier && !customer){
alert('hide table');
}
if( supplier & customer == false){
//alert
}
$('.supplier, .customer').on('click', function(){
var supplier = $('.supplier').is(":checked");
var customer = $('.customer').is(":checked");
// if both is unchecked, hide the table
if( !supplier && !customer){
alert('hide table');
}
// if supplier is checked, show supplier, else hide supplier
});