找到邮政编码时验证邮政编码和打印状态
Validate Zipcode and Print State when Zipcode is found
此应用程序的主要 objective 是提供邮政编码搜索,然后在找到邮政编码后显示与邮政编码关联的州。我如何修改此代码以反映我要实现的目标?
<input type="text" id="zipCode" placeholder="ZIP code" onKeyUp="validateZip()"/>
<div id="msg"></div>
function checkIfAvailable(zip)
{
let zones = [["90210","Beverly Hills"],
["90211","BH"]]
return( zones.indexOf(zip) >= 0 )
}
function validateZip()
{
let zip = document.getElementById("zipCode").value;
let msg =""
if(checkIfAvailable(zip)
{
msg="Our service is available in" + State
;
}
else
{
msg="Sorry our service is not available in this area";
}
document.getElementById("msg").innerHTML = msg;
}
如果您可以将 array
更改为 object
,它会很简单:
let zones = {90210: "Beverly Hills", 90211:"BH"};
let msgElement = document.getElementById("msg")
function validateZip(userInput) {
if (userInput.value in zones) {
msgElement.innerHTML = "Our service is available in " + zones[userInput.value];
} else {
msgElement.innerHTML = "Sorry our service is not available in this area";
}
}
<input type="text" id="zipCode" placeholder="ZIP code" onKeyUp="validateZip(this)"/>
<div id="msg"></div>
checkIfAvailable
可以使用 find
到 return 内部数组,如果存在:
function checkIfAvailable(zip) {
let zones = [...];
// Find an inner array where the first element [0]
// is the zip
return zones.find(item => item[0] === zip);
}
然后在validateZip
:
const zone = checkIfAvailable(zip);
if (zone) {
// zone will be the matching inner array
// such as ["90210", "Beverly Hills"]
const State = zone[1];
msg = "Our service is available in " + State;
}
此应用程序的主要 objective 是提供邮政编码搜索,然后在找到邮政编码后显示与邮政编码关联的州。我如何修改此代码以反映我要实现的目标?
<input type="text" id="zipCode" placeholder="ZIP code" onKeyUp="validateZip()"/>
<div id="msg"></div>
function checkIfAvailable(zip)
{
let zones = [["90210","Beverly Hills"],
["90211","BH"]]
return( zones.indexOf(zip) >= 0 )
}
function validateZip()
{
let zip = document.getElementById("zipCode").value;
let msg =""
if(checkIfAvailable(zip)
{
msg="Our service is available in" + State
;
}
else
{
msg="Sorry our service is not available in this area";
}
document.getElementById("msg").innerHTML = msg;
}
如果您可以将 array
更改为 object
,它会很简单:
let zones = {90210: "Beverly Hills", 90211:"BH"};
let msgElement = document.getElementById("msg")
function validateZip(userInput) {
if (userInput.value in zones) {
msgElement.innerHTML = "Our service is available in " + zones[userInput.value];
} else {
msgElement.innerHTML = "Sorry our service is not available in this area";
}
}
<input type="text" id="zipCode" placeholder="ZIP code" onKeyUp="validateZip(this)"/>
<div id="msg"></div>
checkIfAvailable
可以使用 find
到 return 内部数组,如果存在:
function checkIfAvailable(zip) {
let zones = [...];
// Find an inner array where the first element [0]
// is the zip
return zones.find(item => item[0] === zip);
}
然后在validateZip
:
const zone = checkIfAvailable(zip);
if (zone) {
// zone will be the matching inner array
// such as ["90210", "Beverly Hills"]
const State = zone[1];
msg = "Our service is available in " + State;
}