JavaScript搜索不允许零

时间:2013-09-23 14:05:19

标签: javascript

请参阅Sample Fiddle

如果您在搜索框中输入任一示例代码,您将获得在jQuery UI对话框中弹出的结果。

第一个例子是 006

以下是代码......

if (ccode == 006) {
    sarcomment = '006';
    sardefinition = 'If you need to make corrections to your information, you may either make them online at www.fafsa.gov, or by using this SAR. You must use your Federal Student Aid PIN to access your record online. If you need additional help with your SAR, contact your school’s financial aid office or visit www.fafsa.gov and click the “Help” icon on the FAFSA home page. If your mailing address or e-mail address changes, you can make the correction online or send in the correction on your SAR. ';
    saractionneeded = 'N/A';
}

在此之后,您会立即看到代码 030 的代码。

以下是代码......

if (ccode == 030) {
    sarcomment = '030';
    sardefinition = 'We are unable to read all of the information on your FAFSA or SAR because it was damaged. Please review all of the items on this SAR and make any corrections as needed.';
    saractionneeded = 'N/A';
}

代码006和030的设置是相同的。我在这里学到的是,我创建的任何以0(零)结尾的搜索条件都将导致未定义的查询。

不确定如何解决此问题并寻求您的帮助。

2 个答案:

答案 0 :(得分:4)

旧&中以0 开头的数字向后兼容的JavaScript版本为 octal

030 = 0*8^2 + 3*8^1 + 0*8^0 = 24

Strict mode将八进制数转换为语法错误

答案 1 :(得分:1)

以下是清理该代码的建议。而不是一长串的if语句 - 每一个都提供了一些微妙的bug进入的机会 - 你可以改为使用一个对象将代码映射到信息块上。这看起来像这样:

function showc_code(ccode){
  var codeTable = {
    '006': {
      definition: 'If you need to make corrections to your information, you may either make them online at www.fafsa.gov, or by using this SAR. You must use your Federal Student Aid PIN to access your record online. If you need additional help with your SAR, contact your school’s financial aid office or visit www.fafsa.gov and click the “Help” icon on the FAFSA home page. If your mailing address or e-mail address changes, you can make the correction online or send in the correction on your SAR. ',
      action: 'N/A'
    },
    '030': {
      definition: 'We are unable to read all of the information on your FAFSA or SAR because it was damaged. Please review all of the items on this SAR and make any corrections as needed.',
      action: 'N/A'
    },
    '040': {
      definition: 'Whatever',
      action: 'Something something'
    },
     // ... other codes ...
  };

  if (codeTable[ccode] != null) {
    sarcomment = ccode;
    sardefinition = codeTable[ccode].definition;
    saractionneeded  = codeTable[ccode].action;
  }
  else {
    // unknown code ... do whatever
  }

  // ... rest of your code to show the dialog ...
}

这样,从代码到相关信息的映射只是数据,没有“移动部件”。