如果/ Else jQuery要解析邮政编码列表 [英] If / Else jQuery to parse against a list of zip codes

查看:138
本文介绍了如果/ Else jQuery要解析邮政编码列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个价格报价估算器,它使用一系列邮政编码作为服务区域。邮政编码列表相当广泛,所以我不知道它是否更好,或者根本没有选择拥有外部来源,例如CSV。

I'm trying to write a price quote estimator that utilizes an array of zip codes to be a service area. The list of zip codes is rather extensive, so I don't know if it is better or an option at all to have an external source, such as a CSV.

我希望人们输入他们的邮政编码,如果它在列表中,他们将获得价格计算器选项。

What I would like is for people to enter their zip codes, and if it is on the list, they will be given the price calculator option.

如果他们的邮政编码不在列表中,则会显示一条消息请致电更多信息

If their zip code is not on the list, a message should appear of "Please Call for More Info"

我是排序现在是frankensteining代码,但这就是我所拥有的:

I am sort of frankensteining code right now, but this is what I have:

$(function(){
var zipCodes = ['92056', '90210', '92121', '92101','19148'];
$('#id_div_one, #id_div_two').hide();
if(jQuery.inArray(zipCodes) > -1) {
$('#id_div_one').css('display', 'inline');
} else {
$('#id_div_two').css('display', 'inline');
}
return false;
});

然后这是div:

<form  id="zip_form">
Zip Code: <input type="text" name="zipcode" id="id_zip_code"><input type="submit"    
value="Submit">
</form>

<div id="id_div_one">[CP_CALCULATED_FIELDS id="6"]</div>
<div id="id_div_two">Please call our office for a quote</div>

我让它在提交触发显示价格计算插件的地方工作,但我不明白使用if / else从数组中提取。非常感谢任何帮助 - 我一直在网上搜索没有太多运气。

I had it working where submit was triggering the display of the price calculation plugin, but am not understanding how to work with the if / else to pull from the array. Any help is greatly appreciated -- I have been scouring the web without much luck.

推荐答案

$。 inArray() 需要至少2个参数 - 1)要查找的值和2)要查看的数组的。 文档

$.inArray() requires a minimum of 2 arguments - 1) a value to look for and 2) an array to look into. Documentation

在您的情况下,您只传递值(根据 inArray 的签名的第一个参数),您需要的是:

In your case, you are passing only the value (the first argument as per the signature of inArray), what you need is:

if(jQuery.inArray($('#id_zip_code').val(), zipCodes) > -1) {
    ...
}

这篇关于如果/ Else jQuery要解析邮政编码列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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