如何在 JQuery 中使用 IndexOf

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

How to use IndexOf in JQuery

javascriptjqueryhtmlblurindexof

提问by Blessing Thinker

if($('#this').val().indexOf('4289')){
    Do something
else
    Do something. 

This works only with that 4289,
When I try to add other numbers to be indexed next to it using 'or', it doesn't work. How should I put other number. E.g

这只适用于那个4289
当我尝试添加其他数字以使用“或”在它旁边建立索引时,它不起作用。其他号码应该怎么填。例如

IndexOf('4289||78843') 

I want this to check this numbers and if the number in the input field is not one of this, to echo error.

我希望它检查这些数字,如果输入字段中的数字不是其中之一,则回显错误。

Here's more which happens to die when one revisits the field.

当人们重访该领域时,这里有更多的人会死。

$('#Zip').blur(function(){
        if (($(this).val().indexOf('0860') > -1)||($(this).val().indexOf('0850') > -1)){
        $('#Status_Zip').html("No way.")
        $(this).alterClass('*_*', 'Success')
        return false;
        }else{$('#Status_Code').hide()
        $(this).alterClass('*_*', 'Error')
        $(this).css('border-color', '#F00').css('background-color', '#FFC').effect("pulsate",{times:4},2)
            return true;
        }
    })

回答by David

That's because it would be looking for the string '4289||78843', which doesn't exist in the target I'm assuming. Logical operators can't just be tossed in anywhere, only where there are actual values to logically operate on. Something like this:

那是因为它会寻找 string '4289||78843',它在我假设的目标中不存在。逻辑运算符不能随便扔在任何地方,只能在有实际值的地方进行逻辑运算。像这样的东西:

if(($('#this').val().indexOf('4289') > -1) ||
   ($('#this').val().indexOf('78843') > -1))

The return value of the indexOf()function is the numeric index of that value in the target value, or -1 if it's not found. So for each value that you're looking for, you'd want to check if it's index is > -1(which means it's found in the string). Take that whole condition and ||it with another condition, and that's a logical operation.

indexOf()函数的返回值是该值在目标值中的数字索引,如果未找到,则返回 -1。因此,对于您要查找的每个值,您需要检查它的索引是否为> -1(这意味着它在字符串中找到)。把整个条件和||另一个条件结合起来,这是一个合乎逻辑的操作。

Edit:Regarding your comment, if you want to abstract this into something a little cleaner and more generic you might extract it into its own function which iterates over a collection of strings and returns trueif any of them are in the target string. Maybe something like this:

编辑:关于你的评论,如果你想把它抽象成更简洁、更通用的东西,你可以将它提取到它自己的函数中,该函数迭代字符串集合,true如果其中任何一个在目标字符串中,则返回。也许是这样的:

function isAnyValueIn(target, values) {
    for (var i = 0; i < values.length; i++) {
        if (target.indexOf(values[i]) > -1) {
            return true;
        }
    }
    return false;
}

There may even be a more elegant way to do that with .forEach()on the array, but this at least demonstrates the idea. Then elsewhere in the code you'd build the array of values and call the function:

.forEach()在数组上甚至可能有更优雅的方式来做到这一点,但这至少证明了这个想法。然后在代码的其他地方构建值数组并调用函数:

var values = ['4289', '78843'];
var target = $('#this').val();
if (isAnyValueIn(target, values)) {
    // At least one value is in the target string
}