jQuery 带返回语句的三元运算符 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19439219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:57:55  来源:igfitidea点击:

Ternary operator with return statements JavaScript

javascriptjqueryternary

提问by JDillon522

I need to return true or false if an option in a drop down selected.

如果选择了下拉列表中的选项,我需要返回 true 或 false。

This is my code:

这是我的代码:

var active = sort.attr('selected') ? return true : return false;

I get an error that the first returnis unexpected.

我收到一个错误,第一个return是意外的。

Why?

为什么?

采纳答案by JDillon522

You cannot assign a return statement to a variable. If you want activeto be assigned the value trueor false, just delete the returns:

您不能将 return 语句分配给变量。如果要active分配值trueor false,只需删除returns:

var active = sort.attr('selected') ? true : false;

or maybe better:

或者更好:

var active = sort.prop('selected');

since .propalways returns trueor false, regardless of the initial tag attribute.

因为.prop总是返回trueor false,无论初始标签属性如何。

回答by George K

You can return from a ternary operator in javascript like this:

您可以像这样从 javascript 中的三元运算符返回:

return sort.attr('selected') ? true : false;

回答by nightograph

why not just this?

为什么不只是这个?

 return sort.attr('selected');

回答by aircraft

From the docs:

从文档:

Syntax
condition ? expr1 : expr2
Parameters
condition (or conditions) An expression that evaluates to true or false.
expr1, expr2
Expressions with values of any type.

语法
condition ? expr1 : expr2
参数
condition (or conditions) 计算结果为真或假的表达式。
expr1, expr2
具有任何类型值的表达式。

You should pay attention to the Expressions with values of any type., the return xxxis not an expression.

你应该注意Expressions with values of any type.,这return xxx不是一个表达式。

From wikipedia:

来自维基百科:

An expression is a syntactic construct, it must be well-formed: the allowed operators must have the correct number of inputs in the correct places, the characters that make up these inputs must be valid, have a clear order of operations, etc. Strings of symbols that violate the rules of syntax are not well-formed and are not valid mathematical expressions.

表达式是一种句法结构,它必须是格式良好的:允许的运算符必须在正确的位置有正确数量的输入,组成这些输入的字符必须是有效的,具有清晰的操作顺序等。 字符串违反语法规则的符号的格式不正确,并且不是有效的数学表达式。

So, in your case you can use:

因此,在您的情况下,您可以使用:

return sort.attr('selected') ? true : false

回答by RobG

Just a comment on your code:

只需对您的代码发表评论:

> sort.attr('selected')

seems to be using the jQuery attrmethod, which used to try and second guess what you wanted and return either the attribute or the property. I think in recent versions it returns the attribute always.

似乎正在使用 jQuery attr方法,该方法用于尝试再次猜测您想要什么并返回属性或属性。我认为在最近的版本中它总是返回属性。

Anyway, the presence of the selectedattribute only means that the item (an option element?) is the defaultselected option, it doesn't mean that it is the currentlyselected option. For that you need the selected property(jQuery propmethod). And since the selectedproperty is a boolean:

无论如何,selected属性的存在仅意味着该项目(选项元素?)是默认选择的选项,并不意味着它是当前选择的选项。为此,您需要 selected属性(jQuery prop方法)。由于selected属性是一个布尔值:

> sort.attr('selected') ? true : return false;

can simply be:

可以简单地是:

 sort.prop('selected');

or without jQuery:

或不使用 jQuery:

 optionElement.selected;