Javascript 未定义函数
我的jquery脚本出现了一个函数未定义错误,我不知道为什么 JQuery代码: http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAhTrgZ5jvdqcEQouEpPcZ_hS81NmJwGXlxuJr8lEEo4Njw3WRVhT8auzZb55JSMDkwIaCdNkPHL5gNg“type=“text/javascript”>Javascript 未定义函数,javascript,jquery,function,undefined,Javascript,Jquery,Function,Undefined,我的jquery脚本出现了一个函数未定义错误,我不知道为什么 JQuery代码: http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAhTrgZ5jvdqcEQouEpPcZ_hS81NmJwGXlxuJr8lEEo4Njw3WRVhT8auzZb55JSMDkwIaCdNkPHL5gNg“type=“text/javascript”> $(文档).ready(函数(){ var dealerName=$('.name','.adr').t
$(文档).ready(函数(){
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(客户名称);
$(#nameAddress.placeholderAddress”).html(customerAddress);
var error_address_empty='请先输入有效地址';
var error_invalid_address='此地址无效。请确保同时输入您的街道号码和城市?';
var error_google_error='处理您的请求时出现问题,请重试';
var error_no_map_info='抱歉!此地址没有可用的映射信息';
var default_address=客户地址;
var当前地址=空;
var-map=null;
var-geocoder=null;
var-gdir=null;
var-map_-compatible=false;
if(GBrowserIsCompatible()){
map_compatible=true;
}
函数初始化_映射(){
如果(与地图兼容){
map=newGMAP2(document.getElementById('map_canvas');
geocoder=新的GClientGeocoder();
显示地址(默认地址);
addControl(新的gsmallmappcontrol());
addControl(新的GMapTypeControl());
}
}
函数显示地址(地址){
if(地图兼容和地理编码器){
当前地址=地址;
geocoder.getLatLng(
地址:,
功能(点){
如果(!点){
警报(错误\无\地图\信息);
}否则{
地图设定中心(点13);
var标记=新的GMarker(点);
添加覆盖图(标记);
marker.openInfoWindowHtml(“+customerName+”
“+address+”);
}
}
);
}
返回false;
}
函数get_directions(){
如果(与地图兼容){
if(document.direction\u form.from\u address.value==“”){
警报(错误\地址\空);
返回false;
}
document.getElementById('directions')。innerHTML='';
gdir=新的GDirections(map,document.getElementById('directions');
addListener(gdir,'error',handleErrors);
设置_方向(document.direction_form.from_address.value,current_address);
}
返回false;
}
功能集\u方向(从地址到地址){
gdir.load(“从:“+fromAddress+”到:“+toAddress,
{“locale”:“en”});
}
函数句柄错误(){
if(gdir.getStatus().code==G\u GEO\u UNKNOWN\u ADDRESS)
警报(错误地址无效);
else if(gdir.getStatus().code==G\u GEO\u SERVER\u ERROR)
警报(错误\谷歌\错误);
else if(gdir.getStatus().code==G\u GEO\u MISSING\u QUERY)
警报(错误\地址\空);
其他的
警报(错误地址无效);
}
$(窗口).load(初始化映射);
});
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
处理程序中设置了函数。有人告诉我,我需要将函数绑定到该处理程序中的事件,就像初始化映射一样。我试过这样做,但我尝试的一切都不起作用。旁注和有用提示:show_address
在我调用initialize_map
时立即运行以启动映射;但是,当您单击“经销商中心地图”按钮时,它也应该被称为<代码>获取方向仅在您提交表单时调用
到目前为止,当页面加载时,所有内容都会正确显示;但是,当我点击“经销商中心地图”或提交表单时,我会收到一个javascript错误,告诉我函数没有定义
我对这件事束手无策。不知道如何让它正常工作。非常感谢您的帮助。谢谢 这是因为您的
show\u address
和get\u directions
仅在该文档中定义。准备好处理程序的作用域后,如果您想通过内联脚本调用它们,您需要将函数移到该作用域之外。未定义的函数通常会告诉您选择了哪一行
<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>