如何比较 jQuery val() 和 JavaScript 值

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

How to compare jQuery val() and JavaScript value

javascriptjqueryhtml-selecthtml-input

提问by Mikayil Abdullayev

When I do the following comparison I get false although both val() and value visually display the same value:12

当我进行以下比较时,虽然 val() 和 value 在视觉上显示相同的值,但我得到了错误:12

if ($(this).val() == txt.value)//returns false

When I alert both $(this).val() and txt.value I get 12. Is one of them a string and the other one an int? If so, which one's what?

当我同时提醒 $(this).val() 和 txt.value 时,我得到 12。其中一个是字符串,另一个是 int?如果是这样,哪个是什么?

回答by Joseph

Do a typeofto know the types of your values.

做一个typeof了解你的价值观的类型。

console.log(typeof $(this).val());
console.log(typeof txt.value);


jQuery could have altered the values when using .val()like trim whitespaces that should be present. To make sure, you can avoid using val().

jQuery 可能会在使用.val()应该存在的类似修剪空格时更改这些值。为了确保,您可以避免使用val().

The thisin .each()as well as the second argument is the DOM element per iteration. You could get the value of the optiondirectly:

this.each()以及第二个参数是每次迭代的DOM元素。您可以直接获取 的值option

$('select > option').each(function(i,el){
   //we should be getting small caps values
   console.log(this.value);
   console.log(el.value);
});


When using loose comparison (==), number 12 and string 12 should be the same. Even more surprising is that it's true even with the string having whitespaces around it. But with strict comparison (===), they shouldn't be:

使用松散比较 ( ==) 时,数字 12 和字符串 12 应该相同。更令人惊讶的是,即使字符串周围有空格也是如此。但是通过严格的比较 ( ===),它们不应该是:

"12"    ==  12  // true
" 12  " ==  12  // true; tested on Firefox 20 (nightly)
"12"    === 12  // false


At this point, we have weeded all thinkable gotchas. If none worked, both could be totally different values in the first place.

在这一点上,我们已经清除了所有可以想到的问题。如果没有工作,首先两者可能是完全不同的值。

回答by Mikayil Abdullayev

I feel obliged to write so comments, which I find very useful, after taking so much time of the guys around here. So my problem was this:

在花了这么多时间在这里的人之后,我觉得有必要写下这些评论,我觉得这些评论非常有用。所以我的问题是这样的:

('#mySelect option').each(function({
  if ($(this).val() == txt.value)
    return;
});
alert('Code still continues');

Although the result of the condition $(this).val() == txt.value was true the code after the each loop still executed. That's why I thought that the two values did not match. But if I put an alert inside this condition it showed up. It means something wrong was with the behavior of "return" inside the each loop. I've lately come to understand that although return stops the execution of the code after itself inside the each loop, it does not prevent the code from executing after the each block. Thus, to achieve the full function return inside the jquery each loop we seem to have to put a flag and decide if to return after the each according to this flag. The following code worked for me:

尽管条件 $(this).val() == txt.value 的结果为真,但每个循环之后的代码仍然执行。这就是为什么我认为这两个值不匹配的原因。但是如果我在这种情况下发出警报,它就会出现。这意味着每个循环内的“返回”行为有问题。我最近才明白,虽然 return 在 each 循环内停止了代码的执行,但它并没有阻止代码在 each 块之后执行。因此,为了在 jquery each 循环中实现完整的函数返回,我们似乎必须放置一个标志,并根据这个标志决定是否在 each 之后返回。以下代码对我有用:

var shouldContinue = true;
('#mySelect option').each(function ()
{
    if ($(this).val() == txt.value) {
        shouldContinue = false;
        return (false);
    }
});
if (!shouldContinue)
    return; 

Note return(false) inside the iteration. What this means is, if the condition is true continue iterating no more and leave the loop.

注意迭代中的 return(false)。这意味着,如果条件为真,则不再继续迭代并离开循环。