【发布时间】:2017-10-20 17:20:37
【问题描述】:
我想知道将以下内容编写为三元运算符的最干净的方法是什么:
if (jQuery('#product-options-wrapper select').val() || jQuery('#product-options-wrapper input').val()) {
return true;
} else {
return false;
}
【问题讨论】:
我想知道将以下内容编写为三元运算符的最干净的方法是什么:
if (jQuery('#product-options-wrapper select').val() || jQuery('#product-options-wrapper input').val()) {
return true;
} else {
return false;
}
【问题讨论】:
这里基本上不需要三元运算符。
你会使用:
jQuery('#product-options-wrapper select').val() || jQuery('#product-options-wrapper input').val() ? true : false
这与:
jQuery('#product-options-wrapper select').val() || jQuery('#product-options-wrapper input').val()
如果你想退货,你可以使用:
return !!(jQuery('#product-options-wrapper select').val() || jQuery('#product-options-wrapper input').val());
注意我使用的
!!将值转换为布尔值(真/假)
【讨论】:
!! 放在中间块中以使其上面的句子为真。
if,代码将起作用,但如果你使用(if a === true),你是对的,它不会起作用。这就是我添加演员表的原因。
"if(jQuery('#product-options-wrapper select').val() || jQuery('#product-options-wrapper input').val()) ? return true : return false;" 会做同样的事情
【讨论】: