简体   繁体   English

jQuery字符串比较在IE7中的行为有所不同

[英]jQuery string comparison behaves differently in IE7

The following snippet is used in conjunction with a jQuery UI autocomplete element ( #search ). 以下代码段与jQuery UI自动完成元素( #search )结合使用。 In Firefox, Chrome, etc. it behaves as expected and always returns true for the selected element. 在Firefox,Chrome等中,它的行为符合预期,并且对于所选元素始终返回true。 In Internet Explorer 7 it doesn't . 在Internet Explorer 7中没有

$('mySelector').filter(function() {
    if ($(this).text().toLowerCase() == $('#search').val().toLowerCase()) {
         return true;
    }
});

For any hints about how this behaviour could be caused I will be very thankful! 对于如何导致这种行为的任何提示,我将非常感谢!

EDIT: After pasting the nice analyze function by Šime Vidas I run the thing again and here comes the result of the comparison that should return true: 编辑:在通过ŠimeVidas粘贴了不错的分析函数之后,我再次运行了该操作,并且比较结果应返回true:

After some more investigation. 经过更多调查。 I seems the comparison returns true (thank god, otherwise I would have needed a shrink). 我似乎比较结果返回true(感谢上帝,否则我将需要缩小)。 But the filter function return any valid objects. 但是过滤器函数返回任何有效对象。 Which it should, if the comparison is true. 如果比较为真,则应选择哪个。

EDIT: Turns out I tested only cases where everything was fine. 编辑:原来我只测试了一切都很好的情况。 A few entries had double spaces between first and last name which didn't result in a FALSE evaluation in FF and Chrome but did in IE7. 一些条目的名字和姓氏之间有双倍空格,这在FF和Chrome中不会导致FALSE评估,而在IE7中会导致。

Here: 这里:

function analyze( str ) {
    var output, i;

    output = 'String: ' + str + ' - Length: ' + str.length + '; ';

    for ( i = 0; i < str.length; i += 1 ) {
        output += str.charCodeAt( i ) + ' ';
    }

    return output;  
}

And then: 接着:

alert( analyze( operand1 ) + '\n\n' + analyze( operand2 ) );

Live demo: http://jsfiddle.net/jsZzY/ 现场演示: http //jsfiddle.net/jsZzY/

The alert box will show you all code points of both strings.... 警报框将显示两个字符串的所有代码点。

Maybe $(this).text() in IE7 returns some additional chars. 也许IE7中的$(this).text()返回一些额外的字符。 For example extra spaces, new lines ( \\r\\n ) etc. Try to output $(this).text() and $('#search').val() and visual compare this two strings. 例如多余的空格,换行( \\r\\n )等。尝试输出$(this).text()$('#search').val()并直观比较这两个字符串。

Also you can try to add jQuery.trim functions, etc. If you rid out from all garbage your code should work. 您也可以尝试添加jQuery.trim函数等。如果您摆脱了所有垃圾,您的代码应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM