自动选择<选项>基于输入字段,有一些注意事项选项>
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用正则表达式来提取值...
然后,对于像 DD 这样的范围的代码,也许是这样的(伪代码)...
所以,对于 DD5,这将返回
DD1
,对于DD11
,它将返回DD8
。 像B2
或BA3
这样的东西将分别简单地返回B
和BA
。如果您有多个具有不同范围的其他代码,您可以将
if
更改为switch
。 然后,只需将具有该值的列表项设置为当前选择即可。You can use a regular expression to pull out the values...
Then, for a code with a range like DD, perhaps something like this (pseudo-code)...
So, for
DD5
, this will returnDD1
and forDD11
it will returnDD8
. Something likeB2
orBA3
will simply returnB
andBA
, respectively.You could change the
if
to aswitch
if you have multiple other codes with different ranges. Then, just set the list item with that value as the current selection.替换:
为:
Replace:
With:
您可以将 zipValue 的开头与选项值进行比较。 不需要正则表达式。 只需使用indexOf。
You can compare the begining of the zipValue with the options values. No need for regular expressions. Just use indexOf.
我不确定这在 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:
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.