我有以下JavaScript代码:
<script type='text/javascipt' language="javascript">
function getUserLoc() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(displayResult, displayError);
}
else {
setMessage("Geolocation is not supported by this browser");
}
}
function displayResult(position) {
setMessage("Latitude: " + position.coords.latitude + ", Longitude: " + position.coords.longitude);
}
function setMessage(msg) {
document.forms[0].myLoc.value = msg;
}
function displayError(error) {
var errors = { 1: 'Permission denied', 2: 'Position unavailable', 3: 'Request timeout' };
setMessage("Error occured: " + errors[error.code]);
}
</script>
与ASP.NET中的一样:
<asp:Button ID="LoginButton" runat="server" Text="Log In" ValidationGroup="LoginUserValidationGroup"
OnClientClick="getUserLoc( )" />
但是当我点击“LoginButton”时,我收到了JavaScript错误:getuserlock is undefined
答案 0 :(得分:5)
您的代码中存在拼写错误。你有:
<script type='text/javascipt' language="javascript">
但它应该是
<script type='text/javascript'>
(文本/ JavaScript的)
此外,删除方法调用中不必要的空间,因为它可以给人一种应该输入参数的印象。 (这不是必需的,但更多的是个人偏好)
OnClientClick="getUserLoc();"