0

我在不同网站的帮助下编写了以下代码。代码片段如下:

function getLocation()
{
   var visitorGeolocation = new geolocate(false, true, 'visitorGeolocation');

//Check for cookie and run a callback function to execute after geolocation is read either from cookie or IPInfoDB API
   var callback = function()
    {
      var city=document.getElementById("city");
      city.value = visitorGeolocation.getField('cityName');
      alert('Visitor city name : ' + visitorGeolocation.getField('cityName'))
    };
visitorGeolocation.checkcookie(callback);
}
</script>
</head>
<body onload="getLocation()">
<form name="frm" method="post" action="result.jsp?page=1">
<input type="text" name="myQuery">
<input name="city" id="city" type="hidden" value="city">
<input type="submit" name="submit" value="Submit">
</form>

我想在提交表单时静默传递 city 的值,以便我可以在结果页面中获取 city 的值。这就是为什么我将其类型设置为“隐藏”。但是我不明白如何在提交表单时设置城市的值,因为我在回调函数中获取了城市的值,如上所示。我不知道该怎么做。

我是网络编程新手。我对回调函数知之甚少。因此,如果有人帮助我解决这个问题,我将不胜感激。谢谢你。

4

1 回答 1

1

我认为有一个困境,因为有一个

  1. <input name="city" id="city" type="hidden" value="city">value=city
  2. var city=document.getElementById("city"); city.value = visitorGeolocation.getField('cityName');

然后我建议, remove value="city"inside <input name="city" id="city" type="hidden">, remove name="submit" value="Submit"or only name="submit"if you have to change the text's button。

使用此功能:

function getLocation()
{
   var visitorGeolocation = new geolocate(false, true, 'visitorGeolocation');

//Check for cookie and run a callback function to execute after geolocation is read either from cookie or IPInfoDB API
   var callback = function()
    {
      document.getElementById("city").value = visitorGeolocation.getField('cityName');
      alert('Visitor city name : ' + visitorGeolocation.getField('cityName'));
    };
visitorGeolocation.checkcookie(callback);
}
于 2013-06-05T09:08:20.183 回答