我是使用 javascript 的新手,这是有人放入 C# ASP.Net 网站的代码,它应该从输入的邮政编码中获取城市和州。它在 IE9、Chrome、Firefox 中运行良好,但在 IE8 中失败。我不知道为什么。这是javascript,我希望有些东西对某人来说很突出。我还包括了使用它的标记。我不知道它在哪里失败,我得到 Pcodefail 结果并显示“* 无法找到邮政编码”。
<script type="application/javascript">
$(document).ready(function () {
$('#Pcodefail').hide();
});
</script>
<script type="application/javascript">
$(document).ready(function () {
$('[id$=uxPostalCode]').focus(function () {
var city = $('[id$=uxCityState]');
city.val('');
});
$('[id$=uxPostalCode]').blur(function () {
var city = $('[id$=uxCityState]');
var oCity = $('[id$=uxCity]');
var oState = $('[id$=uxState]');
$.getJSON("http://www.geonames.org/postalCodeLookupJSON?&country=US&callback=?", { postalcode: this.value }, function (response) {
//$.getJSON("http://api.geonames.org/findNearbyPostalCodesJSON?postalcode=?&country=US&radius=10&username=demo", { postalcode: this.value }, function(response) {
if (!city.val() && response && response.postalcodes.length && response.postalcodes[0].placeName) {
city.val(response.postalcodes[0].placeName + ', ' + response.postalcodes[0].adminCode1);
oCity.val(response.postalcodes[0].placeName);
oState.val(response.postalcodes[0].adminCode1);
$('#Pcodefail').hide();
}
else {
$('#Pcodefail').toggle();
}
});
});
});
</script>
HTML
<div class="base-container-controls-100pct">
<div class="base-container-controls-20pct">
<div class="label-sm">Postal Code<span style="color: #ff0000; vertical-align: top">*</span> </div>
</div>
<div class="base-container-controls-50pct">
<asp:TextBox ID="uxPostalCode" CssClass="textbox-sm" Text="" TabIndex="7" runat="server" MaxLength="11"></asp:TextBox>
<asp:RequiredFieldValidator ID="uxreqValZip" runat="server" ErrorMessage="Postal Code" Font-Bold="False" Text="*" ValidationGroup="one" ControlToValidate="uxPostalCode"></asp:RequiredFieldValidator>
<div id="Pcodefail" style="color: Red">* Unable to find Postal Code</div>
</div>
</div>