首页 \ 问答 \ 需要帮助比较数组与var(Need help comparing an array against a var)

需要帮助比较数组与var(Need help comparing an array against a var)

嗨,我似乎无法做到这一点。 基本上,如果选择列表的值是“USA”,“CAN”或“MEX”,则它应该返回true。 任何其他值都应返回false。 如果值为“USA”,则仅返回true

$.validator.addMethod(
"ReutersNA",
function(value, element) {
    var selectedCountry = $("#Country").val();
    var NorthAmerica = new Array("USA","CAN","MEX");
    if($.inArray(selectedCountry,NorthAmerica)) {
        return false;
    } else return true;
}, "Cannot select Reuters News outside of North America."
);

Hi I can't seem to get this right. Basically if the value of a select list is either "USA", "CAN", or "MEX" it should return true. any other value should return false. This only returns true if the value is "USA"

$.validator.addMethod(
"ReutersNA",
function(value, element) {
    var selectedCountry = $("#Country").val();
    var NorthAmerica = new Array("USA","CAN","MEX");
    if($.inArray(selectedCountry,NorthAmerica)) {
        return false;
    } else return true;
}, "Cannot select Reuters News outside of North America."
);

原文:https://stackoverflow.com/questions/3215927
更新时间:2019-06-23 01:16

最满意答案

我不是Jquery专家,但这是来自.inArray的Jquery站点 ,你应该知道inArray返回索引并且:

“因为JavaScript将0视为松散等于false(即0 == false,但是0!== false),如果我们检查数组中是否存在值,我们需要检查它是否等于(或更大)比)-1。“

因此,您应该在if语句中尝试!= -1> -1 ,如下所示

if($.inArray(selectedCountry,NorthAmerica) != -1)  

要么

if($.inArray(selectedCountry,NorthAmerica) > -1)  

I am not a Jquery expert but this is from the Jquery Site for .inArray, You should be aware that inArray returns the index and:

"Because JavaScript treats 0 as loosely equal to false (i.e. 0 == false, but 0 !== false), if we're checking for the presence of value within array, we need to check if it's not equal to (or greater than) -1."

Therefore you should be trying != -1 or >-1 in your if statement as follows

if($.inArray(selectedCountry,NorthAmerica) != -1)  

or

if($.inArray(selectedCountry,NorthAmerica) > -1)  
2010-07-09

相关问答

更多

检查Array的字符串是否显示为另一个数组字符串的一部分(Check if an Array's strings appear as a part of another array's strings)

使用array.prototype.map和array.prototype.filter获取帮助: var inputArray = [ "Bob Johnson goes to the zoo", "Timothy Smith likes to eat ice-cream", "Jenny wants to play with her friends", "There is no one in the room to play with Timothy", "Jeremy J ...

需要帮助创建一个按钮来激活javascript函数(Need help creating a button to activate a javascript function)

只需添加: function randomImage() { var newImage = getRandomImage(); console.log(newImage); document.body.appendChild(newImage); }; 并点击按钮添加: <input type="button" value="More Winry" onclick="randomImage()"> 编辑要替换现有图像: function randomImage() { var newImag ...

WordPress的get_query_var()(WordPress get_query_var())

我设法让get_query_var()函数起作用。 要成功使用这两个函数,您需要将查询变量添加到wordpress的查询变量数组中。 这是一个代码示例。 function add_query_vars_filter( $vars ){ $vars[] = "query_var_name"; return $vars; } //Add custom query vars add_filter( 'query_vars', 'add_query_vars_filter' ); 现在您可以使用 ...

Java Array帮助(Java Array help)

在一个循环中查找索引并将它们放入一个集合中(例如ArrayList最低)。 然后使用Collections.shuffle(最低)并选取第一个元素lowest.get(0)。 这应该做你想做的。 Find the indexes in a loop and put them in a collection (e.g. ArrayList lowest). Then use Collections.shuffle(lowest) and pick the first element lowest.g ...

var_dump仅在上传多个文件时显示1个数组(var_dump only shows 1 array when upload more than 1 file)

与使用多于一次的任何其他表单元素相同: <input type="file" name="filename[]" multiple="multiple"/> Same as any other form element used more then once: <input type="file" name="filename[]" multiple="multiple"/>

通过比较Javascript中的2个2D数组来查找缺少的元素(Find missing element by comparing 2 2D-Arrays in Javascript)

您也可以像这样重新排列数组: var sml = {}; sml["dean"] = 22; sml["james"] = 31; sml["ludwig"] = 35; var lrg = {}; lrg["dean"] = 22; lrg["james"] = 31; lrg["ludwig"] = 35; lrg["kevin"] = 23; lrg["elton"] = 40; 并使用: function findDeselectedItem(c,p){ ret=[]; ...

将数组键与预定义数组进行比较 - Ruby(Compare array of keys against predefined array - Ruby)

@response.each_value do |value| expect(value.keys).to match_array(key_array) end 这对你有用。 @response.each_value do |value| expect(value.keys).to match_array(key_array) end This will work for you.

在Ruby中基于数组中的索引比较项目(Comparing items based on their index in an array in Ruby)

您可以使用array.index方法。 以下代码检查两张卡的索引,如果other张卡出现在当前卡之后,则返回true 。 class Card @@RANKS = ['A', 'K', 'Q', 'J', 'T', '9', '8','7','6','5','4','3','2'] attr_reader :rank def initialize(str) @rank = str[0,1] end def > (other) @@RANKS.index(ot ...

相关文章

更多

最新问答

更多
  • 我用这台扫描仪做错了什么?(What am I doing wrong with this scanner? [duplicate])
  • 我想取个意大利名
  • getline似乎无法正常工作(getline seems to not working correctly)
  • 期望'const char * __restrict__'但参数类型为'int'(expected ‘const char * __restrict__’ but argument is of type ‘int’)
  • 固定侧栏导航在流体twitter bootstrap 2.0(Fixed sidebar navigation in fluid twitter bootstrap 2.0)
  • 通过两个字符串对象创建日期/时间对象(Creating a date/time object by two string objects)
  • Javascript获取JSON数据的问题(Issues with Javascript getting JSON data)
  • Android宽度:100%修复(网站接管问题)(Android width:100% fix (website takeover issue))
  • C ++函数/方法设计的良好实践(Good practice in C++ function/method design)
  • 计算其他表中不存在的所有记录 - SQL查询(Count all records that does not exist to other table - SQL Query)
  • 为什么我要用JPA共享ID?(Why do I get shared Ids with JPA?)
  • asp.net - 如何显示来自html格式的数据行的字段(asp.net - how to display a field from data row that is in html format)
  • 我们如何使用ActiveRecord从连接表中删除行?(How can we delete rows from a join table by using ActiveRecord?)
  • ng-class搞乱了类的顺序(ng-class messing with the order of classes)
  • oracle 12g无效数字错误(oracle 12g invalid number error)
  • 更改ng-src值onclick(Change ng-src value onclick)
  • 如何在android中自动添加自定义依赖项以创建新项目?(How to add custom dependencies automatically in android for ever a new project is created?)
  • datetime函数在PHP中(datetime function in php)
  • 在javascript中获取会话数组的值(in javascript get the value of a session array)
  • 如何在UTF8中编译LaTeX?(How can I compile LaTeX in UTF8? [closed])
  • Rspec:“array.should == another_array”,但不用担心订单(Rspec: “array.should == another_array” but without concern for order)
  • Logcat错误:无法在android片段中加载视图(Logcat error: unable to load view in android fragments)
  • JavaFX的。(JavaFX. Adding items to the list in different threads is not working)
  • 从GDATA日历资源迁移到Google Calendar Resource api(Migrate from GDATA calendar resource to Google Calendar Resource api)
  • SSRS 2008 - 以零情景处理分割(SSRS 2008 - Dealing with division by zero scenarios)
  • 我如何以编程方式添加一个listView列标题的点击事件(How can I add a listView column header a click event programmatically)
  • Wxpython:无法检索有关列表控件项XXX的信息(Wxpython: Couldn't retrieve information about list control item XXX)
  • 使用Tortoise SVN在SVN存储库中移动目录(Move Directory across SVN repository using Tortoise SVN)
  • 天蓝色服务结构集群中的web api无状态服务是否在一段时间不活动后进入休眠状态?(Do web api stateless services in azure service fabric cluster go to sleep after a period of inactivity?)
  • 我可以设置intelliJ来突出显示PHP编码风格吗?(Can I set intelliJ to highlight php coding style?)