需要将Javascript生成的值插入到隐藏表单字段中

需要将Javascript生成的值插入到隐藏表单字段中,javascript,Javascript,我正在使用一个地理位置脚本生成基于IP的用户城市 标题代码: <script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script> 生成城市的代码: <script language="JavaScript">document.write(geoip_city());</script> document.write(geoip_city()); 我

我正在使用一个地理位置脚本生成基于IP的用户城市

标题代码:

<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>

生成城市的代码:

 <script language="JavaScript">document.write(geoip_city());</script>
document.write(geoip_city());
我的表单如下所示:

<form name="form1" id="form1" method="post" action="script.php">Email: <input       size="35" type="text" name="email"> 
<input type="hidden" id="city" name="city" value="" >
    </form>
电子邮件:
我需要能够将javascript生成的值插入表单的值字段中

我似乎做不到,我尝试过各种方法,比如在表格后面放上这个:

<script type="text/javascript"> 
document.getElementById("city").value = "1";
alert(document.getElementById("city").value);
</script>

document.getElementById(“城市”).value=“1”;
警报(document.getElementById(“city”).value);
这是一个保存为.php的页面。

您只需插入“1”。尝试:


var city=geoip_city();
写作(城市);


document.getElementById(“城市”).value=city;
警报(document.getElementById(“city”).value);
这可能需要改进-我没有使用过这个库。变量
city
要求您读取位置两次。

该公司似乎是。以下是您如何使用他们的

“严格使用”;
var getCity=函数(geoipResponse){
var cityName=geoipsresponse.city.names.en | | |“您的城市”;
document.getElementById('city')。value=cityName;
警报(城市名称);
};
var fail=功能(错误){
警报(错误错误);
控制台错误(err);
}
geoip2.city(getCity,fail)

电邮:
城市:

您可以将语句更改为

document.form1.city.value="new value";

假设最后一个代码段是示例代码,这将起作用:浏览器控制台是否向您发出任何警告或其他消息?您确定代码是在可以与dom交互的时间执行的吗?
<script type="text/javascript"> 
document.getElementById("city").value = city;
alert(document.getElementById("city").value);
</script>
document.form1.city.value="new value";