我的jquery脚本得到了一个未定义错误的函数,我不知道为什么。
JQuery代码: http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAhTrgZ5jvdqcEQouEpPcZ_hS81NmJwGXlxuJr8lEEo4Njw3WRVhT8auzZb55JSMDkwIaCdNkPHL5gNg“type =”text / javascript“>
<script type="text/javascript">
$(document).ready(function(){
var dealerName = $('.name', '.adr').text();
var customerName = dealerName.slice(0, - 1);
var customerAddress = $('.street', '.adr').text() + ', ' + $('.locality', '.adr').text() + ', ' + $('.state', '.adr').text() + ', ' + $('.zipCode', '.adr').text();
$("#nameAddress .placeholderName").html(customerName);
$("#nameAddress .placeholderAddress").html(customerAddress);
var error_address_empty = 'Please enter a valid address first.';
var error_invalid_address = 'This address is invalid. Make sure to enter your street number and city as well?';
var error_google_error = 'There was a problem processing your request, please try again.';
var error_no_map_info = 'Sorry! Map information is not available for this address.';
var default_address = customerAddress;
var current_address = null;
var map = null;
var geocoder = null;
var gdir = null;
var map_compatible = false;
if( GBrowserIsCompatible() ) {
map_compatible = true;
}
function initialize_map() {
if( map_compatible ) {
map = new GMap2(document.getElementById('map_canvas'));
geocoder = new GClientGeocoder();
show_address(default_address);
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
}
}
function show_address(address) {
if( map_compatible && geocoder ) {
current_address = address;
geocoder.getLatLng(
address,
function( point ) {
if( !point ) {
alert(error_no_map_info);
} else {
map.setCenter(point, 13);
var marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml("<span style='font-size:14px; font-weight:bold;'>" + customerName + "<br /></span><span style='font-size:12px;'>" + address + "</span>");
}
}
);
}
return false;
}
function get_directions() {
if( map_compatible ) {
if( document.direction_form.from_address.value == '' ) {
alert(error_address_empty);
return false;
}
document.getElementById('directions').innerHTML = '';
gdir = new GDirections(map, document.getElementById('directions'));
GEvent.addListener(gdir, 'error', handleErrors);
set_directions(document.direction_form.from_address.value, current_address);
}
return false;
}
function set_directions(fromAddress, toAddress) {
gdir.load("from: " + fromAddress + " to: " + toAddress,
{ "locale": "en" });
}
function handleErrors(){
if( gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS )
alert(error_invalid_address);
else if( gdir.getStatus().code == G_GEO_SERVER_ERROR )
alert(error_google_error);
else if( gdir.getStatus().code == G_GEO_MISSING_QUERY )
alert(error_address_empty);
else
alert(error_invalid_address);
}
$(window).load(initialize_map);
});
</script>
HTML页面中的代码:
<div id="main-map-wrapper">
<div id="seperator"></div>
<table width="100%" height="94">
<tr valign="top">
<td width="33%" colspan="3">
<div id="headertextdiv">
<div id="nameAddress">
<span class="placeholderName" style="font-size:20px; font-weight:bold; line-height:22px;"> </span>
<br />
<span class="placeholderAddress" style="font-size:14px; font-weight:bold; line-height:22px;"> </span>
<br />
<input type="submit" onClick="return show_address('5930 West Plano Pkwy, Plano, Texas, 75093');" value="Center Map on Dealership">
</div>
</div>
</td>
</tr>
</table>
<div id="map_canvas"> </div>
<form name="direction_form" onSubmit="get_directions(); return false;" class="form">
<span style="font-size:18px; font-weight:bold;">Need directions?</span>
<br />
<span style="font-size:14px; margin-top:7px;">Enter your address below to get turn-by-turn directions:</span>
<br />
<input type="text" name="from_address" class="form-field" />
<input type="submit" value="Get Directions" class="form-submit" style="margin-left:10px;" />
</form>
<a name="directions_table"> </a>
<div id="directions"> </div>
</div>
<div class="footer-fix"> </div>
问题: 无论如何,正如您在代码中看到的那样,我首先设置所有变量(包括customerName和customerAddress,在这种情况下是最重要的)。然后我有四个函数,在脚本的最后我初始化地图。除了html页面上的两件事外,一切都有效。有一个“onClick”来使地图居中不起作用,表单提交不起作用。
我相信这是因为我在ready
处理程序中设置了函数。我被告知我需要将函数绑定到该处理程序中的事件,就像我使用initialize_map一样。我试过这样做,但我尝试的一切都行不通。旁注和有用的提示可能是:show_address
在我拨打initialize_map
时立即开始运行地图;但是,当您点击“经销商中心地图”按钮时,它也应该被调用。只有在您提交表单时才会调用get_directions
。
截至目前,一切都在页面加载时显示;然而,当我点击“经销商中心地图”或提交表格时,我收到一个javascript错误,告诉我该功能未定义。
我对此感到沮丧。不确定如何正常运行。任何帮助是极大的赞赏。谢谢!
答案 0 :(得分:16)
这是因为show_address
和get_directions
仅在document.ready
处理程序范围内定义了 ,如果要调用,则需要将函数移到其他位置他们通过内联脚本。