有没有办法使用一段 JavaScript 在两个字符串之间切换?

发布于 2024-09-25 03:23:22 字数 227 浏览 3 评论 0原文

我想

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

用一行代码做类似 But 的事情。我不太记得它是怎么做的,但它涉及问号和冒号......

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

或十年 2024-10-02 03:23:22

尝试:

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

称为三元表达式。

Try:

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

It is called a ternary expression.

水溶 2024-10-02 03:23:22

看吧,没有三元运算符!

以下之所以有效,是因为 Javascript 短路了布尔表达式。

如果something == string1,则评估string2——因为string2是一个truthy值,并且下一个表达式涉及OR 运算无需继续。停止并返回string2

如果 something !== string1 那么它将跳过下一个操作数,因为如果它为 false,则评估下一个操作数(使用 AND)是没有意义的。它将“跳转”到 OR 运算并返回 string1

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

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

如果您希望完成分配:

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

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

但是最终,我最终会使用三元运算符,因为其他程序员可能不清楚此解决方案。如果您来自 Java 或其他语言,由于所有布尔运算符,您可能期望该函数返回布尔值。

Look ma, no ternary operator!

The following works because Javascript short circuits boolean expressions.

If something == string1 then evaluate string2 -- since string2 is a truthy value and the next expression involves the OR operation there is no need to continue. Stop and return string2.

If something !== string1 then 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.

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.

扛刀软妹 2024-10-02 03:23:22

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

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

如问题所示:

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

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()]
)
小镇女孩 2024-10-02 03:23:22

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

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

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

$.fn.toggleVal = function (str1, str2) {
     return this.val(this.val() == str1 && str2 || str1);
};
$("input").toggleVal('string 1', 'string 2');
煞人兵器 2024-10-02 03:23:22

不过,使用三元运算符的解决方案是最具可读性的,您也可以使用 Lodash 中的 xor:

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

对于您的具体情况:

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

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

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

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

终止放荡 2024-10-02 03:23:22

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

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

You mean to use the ternary operator:

something.val((something.val() == 'string1') ? 'string2' : 'string1');
追风人 2024-10-02 03:23:22

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

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

As of jQuery 1.4, you can do this:

something.val(function(index, val) {
    return val == 'string1' ? 'string2' : 'string1';
});
淑女气质 2024-10-02 03:23:22
 something.val( something.val() == 'string1'? 'string2' : 'string1' );

或为了澄清

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

or for clarification

val astring = something.val() == 'string1'? 'string2' : 'string1';
something.val( astring );
情话难免假 2024-10-02 03:23:22

在类似的情况下我的方法是这样的:

let value = 0

const toggleTwoValues = () => 
  value === 0 ? value = 1 : value = 0

My way was this in a similar situation:

let value = 0

const toggleTwoValues = () => 
  value === 0 ? value = 1 : value = 0

白云悠悠 2024-10-02 03:23:22
something.val(something.val() == 'string1' ? 'string2' : 'string1');
something.val(something.val() == 'string1' ? 'string2' : 'string1');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文