javascript如何使用邮政编码搜索网站的状态 [英] How does javascript search for states using zipcodes for a website

查看:100
本文介绍了javascript如何使用邮政编码搜索网站的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是两个部分的问题:



我在javascript中创建邮政编码查询时遇到问题。第一部分是



1)让javascript重新认识邮政编码链接到一个州,并可能维护这些数据。第二部分是



2)让javascript输出显示给用户的第二个字段文本State Result中的状态。



脚本和网站可以在这里找到:



编辑小提琴 - JSFiddle [ ^ ](这包含整个代码,而不是下载。粘贴在JSFiddle上,因为CodeProject的完整代码太长了。类似于pastebin)。



对节点,外部网站的引用(发布外部网站的示例是可以的,代码链接不是),PhP,SQL数据库,外部程序下载,扩展,遗憾的是不允许。此脚本仅包含使用HTML / Javascript语言。



-----这是javascript的一部分----:

Here is the Question in two parts:

I am having problems creating a zip-code look-up in javascript. The first part is

1) having javascript recongnize that a zip-code is linked to a state, and possibly maintain that data. The second part is to

2) Have the javascript output the state in the second field text "State Result" which is displayed to the user.

The script and site can be found here:

Edit fiddle - JSFiddle[^](This contains the entire code, and is not a download. Pasted on JSFiddle as the full code is too long for CodeProject. Similar to pastebin).

References to Nodes, External Websites (posting an external site for examples is okay, linking in code is not), PhP, SQL Databases, external Program downloads,extensions, are unfortunately not allowed. This script consist of using the HTML/Javascript languages only.

-----Here is part of the javascript----:

function validateZIP(zipfield, statefield) {
var valid = "0123456789-";
var hyphencount = 0;





我尝试了什么:





What I have tried:

function GetZip(90001, 90002){
  return California, California
}

document.getElementById("")=GetZip (9001, 9002)



如上所示,我试图使用一个对象通过9001调用California来返回值在一个页面上为用户加利福尼亚。





我尝试过函数的概念,但字典是一个新概念。



我需要一个更短更清洁/更简单的方法来调用在网站上搜索的邮政编码,输出作为相应的状态。


As seen above, I am attempting to use an object to call California by 9001 with the value return being California to the user on a single page.


I have tried the concept of functions, however dictionaries is a new concept.

There are many zipcodes I need a shorter and cleaner/easier way of calling zip-codes to be searched on the site with the output as corresponding states.

推荐答案

参见美国邮政编码 [ ^ ]。


如果您点击此链接免费邮政编码地图,邮政编码查询和邮政编码清单 [ ^ ],你会注意到,关于页面中间,主题是与州,县和城市匹配邮政编码。在本文中,它提供了一些信息,让您意识到,原则上,您的任务是不可能的!



这是引用:
If you follow this link Free ZIP code map, zip code lookup, and zip code list[^], you will note, about mid-page, the topic "Matching ZIP Codes with States, Counties, and Cities". Within this text it give some information that should let you realize that, in principal, your task is impossible!

Here's the quote:
引用:

邮政编码边界将跨越州界线。邮政编码通常不会跨越州界,但有些人(65733,71749和73949是很好的例子)。

the ZIP code "boundary" will cross the state lines. ZIP codes don't usually cross state lines, but some do (65733, 71749, and 73949 are good examples).



那里的信息可以为你制作一个非常高效的引擎 - 但是因为它永远不会是真的你永远无法解决问题(除非你去拉链+4)


There's info there that can make a pretty efficient engine for you - but since it won't always be true you can never fix the problem (unless you go to zip+4)


else if (Statenames>=70001 && Statenames<=71497){
           return "LA"
               }





通过使用else if语句添加范围并通过声明函数使用此行之前的方法,我能够成功创建邮政编码查找。虽然不是100%准确,因为邮政编码可以从一个范围跳到另一个范围,但它获得了95%,并且包括较大的,在较大的范围内丢失的拉链。这也是在没有API /外部代码的情况下完成的。



By adding a range using else if statements and using the method before this line by declaring a function, I was able to successfully create a zipcode lookup. Though not 100% accurate as zip-codes can jump from one range to another, it got 95% of it and including the smaller, lost zips within the range of the bigger ones. This was also done without an API/External code.


这篇关于javascript如何使用邮政编码搜索网站的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆