自动选择<选项>基于输入字段,有一些注意事项

发布于 2024-07-20 08:48:27 字数 1489 浏览 5 评论 0原文

我有一个 SELECT 元素,需要根据在文本字段中输入的邮政编码的前半部分自动选择适当的选项。 英国邮政编码的格式为 AB12 3CD,其中第一部分由代表县的 1-2 个字母和代表县内区域的数字组成。 最后 3 个字符与此问题无关。

对于大多数字段,它仅基于第一个字母,但对于某些选项,它是邮政编码范围。 HTML 应该能最好地解释它:

<select id="country_field">
  <option value="">Select</option>
  <option value="AB">AB (Aberdeen)</option>
  <option value="AL">AL (St. Albans)</option>
  <option value="B">B (Birmingham)</option>
  <option value="BA">BA (Bath)</option>
  ...
  <option value="DD1">DD 1-7 (Dundee)</option>
  <option value="DD8">DD 8-11 (Dundee)</option>
  ...
</select>

当值恰好是两个字母时,我下面的代码当前将选择正确的元素。 但我需要将其扩展以包含单字母代码(伯明翰)和邮政编码范围(邓迪)。 注意:如果有一个解决方案需要特殊值,例如 DD1/DD2 而不是 DD1/DD8,我可以更改选项值。

简而言之:

  • B2 --> 伯明翰
  • BA3 --> 浴
  • DD5 --> 第一邓迪 [DD1]
  • DD11 --> 第二个 Dundee [DD8]

这是我到目前为止的 Javascript...

window.onload = function()
{
  // postcode INPUT field
  var zipInput = document.getElementById( 'zip_field' );
  // county SELECT field
  var ctySelect = document.getElementById( 'county_field' );

  zipInput.onchange = function()
  {
    var zipValue = zipInput.value;
    var ctyOptions = ctySelect.options;
    for ( i = 0; i < ctyOptions.length; i++ )
    {
      if ( zipValue.substring(0,2) == ctyOptions[i].value )
        ctyOptions[i].selected = true;
    }
  }
}

I have a SELECT element in which I need to auto-select the appropriate option based on the first half of a postcode entered in a text field. British postcodes are of the form AB12 3CD, where the first section consists of 1-2 letters representing the county and a number representing the area within the county. The last 3 characters are irrelevant for this question.

For most of the fields it is based on only the first letter(s), but for some options it is a postcode range. The HTML should explain it best:

<select id="country_field">
  <option value="">Select</option>
  <option value="AB">AB (Aberdeen)</option>
  <option value="AL">AL (St. Albans)</option>
  <option value="B">B (Birmingham)</option>
  <option value="BA">BA (Bath)</option>
  ...
  <option value="DD1">DD 1-7 (Dundee)</option>
  <option value="DD8">DD 8-11 (Dundee)</option>
  ...
</select>

My code below will currently select the correct element when the value is exactly two letters. But I need to expand it to encompass the single-letter codes (Birmingham) and the postcode ranges (Dundee). Note: I can change the option values if there is a solution that warrants special values, e.g. DD1/DD2 instead of DD1/DD8.

In short:

  • B2 --> Birmingham
  • BA3 --> Bath
  • DD5 --> first Dundee [DD1]
  • DD11 --> second Dundee [DD8]

Here's the Javascript I have so far...

window.onload = function()
{
  // postcode INPUT field
  var zipInput = document.getElementById( 'zip_field' );
  // county SELECT field
  var ctySelect = document.getElementById( 'county_field' );

  zipInput.onchange = function()
  {
    var zipValue = zipInput.value;
    var ctyOptions = ctySelect.options;
    for ( i = 0; i < ctyOptions.length; i++ )
    {
      if ( zipValue.substring(0,2) == ctyOptions[i].value )
        ctyOptions[i].selected = true;
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尸血腥色 2024-07-27 08:48:27

您可以使用正则表达式来提取值...

/^([a-z]{1,2})(\d*)\s/i

然后,对于像 DD 这样的范围的代码,也许是这样的(伪代码)...

if(match[1].value == "DD") {   // needs special processing
  range = match[2].value;
  range = range < 8 ? 1 : 8;   // if the number is less than 8, set it to 1, otherwise set it to 8
  listValue = match[1].value + range
} else                         // just use the letters to select the list item 
  listValue = match[1].value;

所以,对于 DD5,这将返回DD1,对于DD11,它将返回DD8。 像 B2BA3 这样的东西将分别简单地返回 BBA

如果您有多个具有不同范围的其他代码,您可以将 if 更改为 switch。 然后,只需将具有该值的列表项设置为当前选择即可。

You can use a regular expression to pull out the values...

/^([a-z]{1,2})(\d*)\s/i

Then, for a code with a range like DD, perhaps something like this (pseudo-code)...

if(match[1].value == "DD") {   // needs special processing
  range = match[2].value;
  range = range < 8 ? 1 : 8;   // if the number is less than 8, set it to 1, otherwise set it to 8
  listValue = match[1].value + range
} else                         // just use the letters to select the list item 
  listValue = match[1].value;

So, for DD5, this will return DD1 and for DD11 it will return DD8. Something like B2 or BA3 will simply return B and BA, respectively.

You could change the if to a switch if you have multiple other codes with different ranges. Then, just set the list item with that value as the current selection.

享受孤独 2024-07-27 08:48:27

替换:

zipInput.onchange = function()
  {
    var zipValue = zipInput.value;
    var ctyOptions = ctySelect.options;
    for ( i = 0; i < ctyOptions.length; i++ )
    {
      if ( zipValue.substring(0,2) == ctyOptions[i].value )
        ctyOptions[i].selected = true;
    }
  }

为:

zipInput.onchange = function()
  {
    var zipValue = zipInput.value.match(/^[a-z]+/gi);

    var ctyOptions = ctySelect.options;
    for ( i = 0; i < ctyOptions.length; i++ )
    {
      if (zipValue[0] === ctyOptions[i].value )
        ctyOptions[i].selected = true;
    }
  }
  1. 首先,我们从循环中删除了变量分配操作。 为什么废物循环重复相同的操作?
  2. 第二,我们现在过滤掉除输入开头的字母之外的所有内容。
  3. 这又可以扩展到包括数字后缀等。

Replace:

zipInput.onchange = function()
  {
    var zipValue = zipInput.value;
    var ctyOptions = ctySelect.options;
    for ( i = 0; i < ctyOptions.length; i++ )
    {
      if ( zipValue.substring(0,2) == ctyOptions[i].value )
        ctyOptions[i].selected = true;
    }
  }

With:

zipInput.onchange = function()
  {
    var zipValue = zipInput.value.match(/^[a-z]+/gi);

    var ctyOptions = ctySelect.options;
    for ( i = 0; i < ctyOptions.length; i++ )
    {
      if (zipValue[0] === ctyOptions[i].value )
        ctyOptions[i].selected = true;
    }
  }
  1. First of all, we removeed the variable assign action from the loop. Why waste cycles repeating the same operation?
  2. Number two, we now filter out everything except the letters in the beginning of the input.
  3. This can in turn be expanded to include the number suffixes, etc.
眼藏柔 2024-07-27 08:48:27

您可以将 zipValue 的开头与选项值进行比较。 不需要正则表达式。 只需使用indexOf。

  zipInput.onchange = function()
  {
    var zipValue = zipInput.value.toUpperCase();
    var ctyOptions = ctySelect.options;
    for ( i = 0; i < ctyOptions.length; i++ )
    {
      if ( zipValue.indexOf(ctyOptions[i].value) == 0 )
        ctyOptions[i].selected = true;
    }
  }

You can compare the begining of the zipValue with the options values. No need for regular expressions. Just use indexOf.

  zipInput.onchange = function()
  {
    var zipValue = zipInput.value.toUpperCase();
    var ctyOptions = ctySelect.options;
    for ( i = 0; i < ctyOptions.length; i++ )
    {
      if ( zipValue.indexOf(ctyOptions[i].value) == 0 )
        ctyOptions[i].selected = true;
    }
  }
笑脸一如从前 2024-07-27 08:48:27

我不确定这在 javascript 中如何工作,但我会执行如下操作:

  • 将您的值设置为正则表达式以匹配您要查找的内容

所以,“B”将变为“^B[0” -9]”(我假设它后面必须跟一个数字)

BA 变为“^BA[0-9]”

DD1 变为“^DD([1-7] )”

DD8 变为“^DD([8- 9] |[1][01] )" 匹配 DD8、DD9、DD10、DD11

然后只需针对您的字符串运行正则表达式(无需对其进行子串,因为 ^ 确保此匹配发生在字符串的开头)并且检查是否匹配成功。

I'm not sure how this would work in javascript but I'd do something like the following:

  • Set up your values to be regular expressions to match what you're looking for

So then, "B", becomes "^B[0-9]" (I'm assuming it must be followed by a number)

BA becomes "^BA[0-9]"

DD1 becomes "^DD([1-7] )"

DD8 becomes "^DD([8-9] |[1][01] )" to match DD8, DD9, DD10, DD11

Then just run the regex against your string (no need to substring it as the ^ makes sure this match occurs at the start of the string) and check if there was a successful match.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文