javascript if - else 快捷方式不起作用?
我有一个复选框,其值为“新”或“现有”公司。
$("input[name=company]:checked").val()
使用 jQuery 显示选中框的值。
现在,如果我这样做:
if ($("input[name=company]:checked").val() == "new") {
is_new_company = true;
} else {
is_new_company = false;
}
alert(is_new_company);
我会得到正确的布尔值。
走捷径:
($("input[name=company]:checked").val() == "new") ? is_new_company = true : is_new_company = false;
我什么也没得到..这是为什么?
I have a checkbox couple with values "new" or "existing" company.
$("input[name=company]:checked").val()
with jQuery shows me the value of the checked box.
Now if I do:
if ($("input[name=company]:checked").val() == "new") {
is_new_company = true;
} else {
is_new_company = false;
}
alert(is_new_company);
I get the correct booleans.
Doing the shortcut:
($("input[name=company]:checked").val() == "new") ? is_new_company = true : is_new_company = false;
I get nothing.. Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你为什么不这样做:
我在 Firebug 控制台上尝试了你的代码,它工作正常:(
input[name=q]
是此页面右上角的搜索框)顺便说一句,我认为以下内容更具可读性(当然,我只将其用于非布尔值):
Why don't you just do:
I tried your code on Firebug console and it works fine: (
input[name=q]
is the search box at the top right corner of this page)By the way, I consider the following to be more readable (I'd use it only for non-boolean values, of course):
尝试
Try