条件简写不起作用?

conditional short hand doesn't work?

var countChecked = $('.member_checkbox:checked').length;
$('.email-btn').text('Email ' + countChecked + 'member' + countChecked > 1 ? 's');

我上面的条件短手有什么问题?如果计数大于 1,我想要的逻辑是添加 's' 到字符串。

The conditional (ternary) operator is the only JavaScript operator that takes three operands. This operator is frequently used as a shortcut for the if statement.

您使用的语法不正确。

语法是

condition ? expr1 : expr2 

您错过了 conditional/ternary operator 的其他部分。

$('.email-btn').text('Email ' + countChecked + 'member' + (countChecked > 1 ? 's' : ''));
//                                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

此外,将条件部分括在括号内。

条件简写的语法是
条件? expr1 : expr2

在你的表达中,
条件 是 $('.email-btn').text('Email ' + countChecked + 'member' + countChecked > 1
表达式 1 是 's'
表达式 2 未给出

此外,
's' 是什么意思,是赋值给什么东西了吗?

var countChecked = $('.member_checkbox:checked').length;
$('.email-btn').text('Email ' + countChecked + 'member' + ((countChecked > 1) ? 's' : ''));

条件语句需要放在括号中,如果您不想附加任何内容,请提供 else 部分,在这种情况下只需附加 ''。