Javascript 检查输入值是否等于数组中的值时出现问题

Javascript 检查输入值是否等于数组中的值时出现问题,javascript,Javascript,我似乎无法让这段代码正常工作(即,让isInArray(userZipCode,chicagoZipCodes)返回true) var chicagoZipCodes = [ 60018, 60068, 60176, 60601, 60602, 60603, 60604, 60605, 60606, 60607, 60608, 60609, 60610, 60611, 60612, 60613, 60614, 60615, 60616, 6

我似乎无法让这段代码正常工作(即,让
isInArray(userZipCode,chicagoZipCodes)
返回
true

  var chicagoZipCodes = [ 60018, 60068, 60176, 60601, 60602, 60603, 60604, 60605, 60606, 60607, 60608, 60609,
                          60610, 60611, 60612, 60613, 60614, 60615, 60616, 60617, 60618, 60619, 60620, 60621,
                          60622, 60623, 60624, 60625, 60626, 60628, 60630, 60631, 60632, 60634, 60636, 60637,
                          60639, 60640, 60641, 60642, 60643, 60644, 60645, 60646, 60647, 60649, 60651, 60652,
                          60653, 60654, 60655, 60656, 60657, 60659, 60660, 60661, 60706, 60707, 60714 ]

  function isInArray(value, array) {
    return array.indexOf(value) !== -1
  }

  elem.bind('change', function(e) {
    userZipCode = $('#zipCode').val();

    alert(isInArray(userZipCode,chicagoZipCodes));

    if ( !(isInArray(userZipCode, chicagoZipCodes)) && (userZipCode.length > 4) ) {
      // success
      alert("Sorry, that is not a Chicago area zip code! :(");
      e.stopImmediatePropagation();
    }
  })

看起来你在比较数字和字符串

function isInArray(value, array) { 
        return array.indexOf(parseInt(value,10)) !== -1 
 }

看起来你在比较数字和字符串

function isInArray(value, array) { 
        return array.indexOf(parseInt(value,10)) !== -1 
 }
在使用indexOf()函数之前,先解析$('#zipCode').val()

return array.indexOf(parseInt(value);
在使用indexOf()函数之前,先解析$('#zipCode').val()

return array.indexOf(parseInt(value);

从DOM返回的所有值都是字符串,因此您需要将它们转换为数字。您可以通过几种方法实现这一点,但最直接的方法是将其用作函数,而不是构造函数

 var chicagoZipCodes = [60018, 60068, 60176, 60601, 60602, 60603, 60604, 60605, 60606, 60607, 60608, 60609,
   60610, 60611, 60612, 60613, 60614, 60615, 60616, 60617, 60618, 60619, 60620, 60621,
   60622, 60623, 60624, 60625, 60626, 60628, 60630, 60631, 60632, 60634, 60636, 60637,
   60639, 60640, 60641, 60642, 60643, 60644, 60645, 60646, 60647, 60649, 60651, 60652,
   60653, 60654, 60655, 60656, 60657, 60659, 60660, 60661, 60706, 60707, 60714
 ]

 function isInArray(value, array) {
   return array.indexOf(value) !== -1
 }

 elem.bind('change', function(e) {
   // All values returned from the DOM are strings
   // so we need to convert them to a number
   userZipCode = Number($('#zipCode').val());

   alert(isInArray(userZipCode, chicagoZipCodes));

   // Check length first since all Chicago zips listed are greater than 4 digits
   // Saves the more expensive check of searching the array for
   //   only zips that are greater than 4
   if ((userZipCode.length > 4) && !(isInArray(userZipCode, chicagoZipCodes))) {
     // success
     alert("Sorry, that is not a Chicago area zip code! :(");
     e.stopImmediatePropagation();
   }
 });

从DOM返回的所有值都是字符串,因此您需要将它们转换为数字。您可以通过几种方法实现这一点,但最直接的方法是将其用作函数,而不是构造函数

 var chicagoZipCodes = [60018, 60068, 60176, 60601, 60602, 60603, 60604, 60605, 60606, 60607, 60608, 60609,
   60610, 60611, 60612, 60613, 60614, 60615, 60616, 60617, 60618, 60619, 60620, 60621,
   60622, 60623, 60624, 60625, 60626, 60628, 60630, 60631, 60632, 60634, 60636, 60637,
   60639, 60640, 60641, 60642, 60643, 60644, 60645, 60646, 60647, 60649, 60651, 60652,
   60653, 60654, 60655, 60656, 60657, 60659, 60660, 60661, 60706, 60707, 60714
 ]

 function isInArray(value, array) {
   return array.indexOf(value) !== -1
 }

 elem.bind('change', function(e) {
   // All values returned from the DOM are strings
   // so we need to convert them to a number
   userZipCode = Number($('#zipCode').val());

   alert(isInArray(userZipCode, chicagoZipCodes));

   // Check length first since all Chicago zips listed are greater than 4 digits
   // Saves the more expensive check of searching the array for
   //   only zips that are greater than 4
   if ((userZipCode.length > 4) && !(isInArray(userZipCode, chicagoZipCodes))) {
     // success
     alert("Sorry, that is not a Chicago area zip code! :(");
     e.stopImmediatePropagation();
   }
 });

感谢所有快速回答的家伙!看起来像是
parseInt
是我想要的--快速简单的解决方案只需注意使用
parseInt
时要小心:感谢所有快速回答的家伙的提醒Jasontanks!看起来像是
parseInt
是我想要的--快速简单的解决方案t请注意,在使用
parseInt
时要小心:感谢Jaston为这个答案添加上下文的提示,我将问题编辑为在
isInArray
中包含
return
,但答案是添加
parseInt
为这个答案添加上下文,我将问题编辑为在中包含
return
ode>isInArray但答案是添加
parseInt