任何使用一段 JavaScript 在两个字符串之间切换的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3807736/
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-10-25 02:19:34  来源:igfitidea点击:

Any way to toggle between two strings using one piece of JavaScript?

javascriptjquerylogic

提问by Bojangles

I want to do something like

我想做类似的事情

if(something.val() == 'string1')
{
     something.val('string2');
}
else if(something.val() == 'string2')
{
    something.val('string1')
}

But in one line of code. I can't quite remember how it's done, but it involves question marks and colons...

但是在一行代码中。我不太记得它是如何完成的,但它涉及问号和冒号......

回答by Matthew Manela

Try:

尝试:

something.val(something.val() == 'string1' ? 'string2' : 'string1');

It is called a ternary expression.

它被称为三元表达式。

回答by Cristian Sanchez

Look ma, no ternary operator!

看,没有三元运算符!

The following works because Javascript short circuits boolean expressions.

以下工作是因为 Javascript 短路布尔表达式。

If something == string1then evaluate string2-- since string2is a truthyvalue and the next expression involves the OR operation there is no need to continue. Stop and return string2.

如果something == string1然后评估string2- 因为string2是一个值并且下一个表达式涉及 OR 操作,所以不需要继续。停下来返回string2

If something !== string1then it will skip the next operand because if it is false, there is no point in evaluating the next operand (with AND). It will "jump" to the OR operation and return string1.

如果something !== string1然后它将跳过下一个操作数,因为如果它为假,则评估下一个操作数(使用 AND)就没有意义。它将“跳转”到 OR 操作并返回string1.

function toggleString(something, string1, string2) {
   return something == string1 && string2 || string1;
}

something.val(toggleString(something.val(), "string1", "string2"));

If you want the assignment done:

如果你想完成任务:

function toggleValue(something, string1, string2) {
   something.val(something.val() == string1 && string2 || string1);
}

toggleValue(something, "string1", "string2"); // something is a jQuery collection

In the end however, I would end up using the ternary operator because this solution might be unclear to other programmers. If you come from Java or other languages, you may expect the function to return a boolean because of all the boolean operators.

然而,最终我会使用三元运算符,因为其他程序员可能不清楚这个解决方案。如果您来自 Java 或其他语言,您可能希望函数返回一个布尔值,因为所有布尔运算符。

回答by kolossal7

Another way to do it using object properties:

另一种使用对象属性的方法:

{ 'string1': 'string2', 'string2': 'string1' }[value]

As in the question:

如问题所示:

something.val(
  { 'string1': 'string2', 'string2': 'string1' }[something.val()]
)

回答by Thierry

Though, the solutions with a ternary operator are the most readable, you could also use xor from Lodash:

虽然使用三元运算符的解决方案是最易读的,但您也可以使用 Lodash 的 xor:

x = _.xor([a, b], [x])[0]

For your specific case:

对于您的具体情况:

something.val(_.xor(['s1',?'s2'],?[something.val()])[0]);

Ref: https://lodash.com/docs/4.17.10#xor

参考:https: //lodash.com/docs/4.17.10#xor

回答by fehays

How about using @Daniel's code along with a jquery function:

如何将@Daniel 的代码与 jquery 函数一起使用:

$.fn.toggleVal = function (str1, str2) {
     return this.val(this.val() == str1 && str2 || str1);
};
$("input").toggleVal('string 1', 'string 2');

回答by Eric

As of jQuery 1.4, you can do this:

从 jQuery 1.4 开始,您可以这样做:

something.val(function(index, val) {
    return val == 'string1' ? 'string2' : 'string1';
});

回答by Mike Axiak

You mean to use the ternary operator:

您的意思是使用三元运算符:

something.val((something.val() == 'string1') ? 'string2' : 'string1');

回答by jcolebrand

 something.val( something.val() == 'string1'? 'string2' : 'string1' );

or for clarification

或澄清

val astring = something.val() == 'string1'? 'string2' : 'string1';
something.val( astring );

回答by James Curran

something.val(something.val() == 'string1' ? 'string2' : 'string1');