函数未定义 - 未捕获的referenceerror [英] Function is not defined - uncaught referenceerror
问题描述
我有这个未被捕获的referenceerror函数没有定义错误哪些不明白。
I have this uncaught referenceerror function is not defined error which do not understand.
如果我有
$(document).ready(function(){
function codeAddress() {
var address = document.getElementById("formatedAddress").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
}
});
}
});
和
<input type="image" src="btn.png" alt="" onclick="codeAddress()" />
<input type="text" name="formatedAddress" id="formatedAddress" value="" />
当我按下按钮时,它将返回未被捕获的参考错误。
When I press the button it will return the "uncaught referenceerror".
但是如果我将codeAddress()放在之外的$(document).ready(function(){}那么它就可以精细。
But if I put the codeAddress() outside the $(document).ready(function(){} then it working fine.
我的意图是将codeAddress()放在document.ready.function中。
My intention is put the codeAddress() within the document.ready.function.
推荐答案
如何删除 onclick
属性并添加ID:
How about removing the onclick
attribute and adding an ID:
<input type="image" src="btn.png" alt="" id="img-clck" />
和你的剧本:
$(document).ready(function(){
function codeAddress() {
var address = document.getElementById("formatedAddress").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
}
});
}
$("#img-clck").click(codeAddress);
});
这种方式如果您需要更改函数名称或任何不需要触及html。
This way if you need to change the function name or whatever no need to touch the html.
这篇关于函数未定义 - 未捕获的referenceerror的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!