如何在JQuery中使用IndexOf [英] How to use IndexOf in JQuery

查看:171
本文介绍了如何在JQuery中使用IndexOf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这仅适用于4289
当我尝试使用或"在其旁边添加其他要索引的数字时,它将不起作用.我该如何输入其他号码.例如

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

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;
        }
    })

推荐答案

这是因为它将查找字符串'4289||78843',该字符串在我假设的目标中不存在.逻辑运算符不能仅仅在任何地方扔,而只能在有实际值要进行逻辑运算的地方抛弃.像这样:

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:

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

indexOf()函数的返回值是该值在目标值中的数字索引,如果未找到则为-1.因此,对于您要查找的每个值,您都需要检查其索引是否为> -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.

编辑:关于您的评论,如果您想将其抽象为更简洁,更通用的内容,则可以将其提取到自己的函数中,该函数遍历字符串集合并返回true如果它们中的任何一个在目标字符串中.也许是这样的:

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 true if any of them are in the target string. Maybe something like this:

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

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

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:

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

这篇关于如何在JQuery中使用IndexOf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆