简体   繁体   English

document.getElementById()。value在表单的隐藏字段ASP.NET网页中不起作用

[英]document.getElementById().value does not work in form's hidden field asp.net web pages

I created a simple website using ASP.NET Web Pages (using Razor markup). 我使用ASP.NET网页(使用Razor标记)创建了一个简单的网站。 I have a form with two hidden fields: latitude and longitude . 我有一个带有两个隐藏字段的表单: latitudelongitude In my JS script I use HTML5 Geolocation API to get user's location expressed in degrees (latitude and longitude) and then try to assign these values to the two hidden fields in the form before sending them out to a server. 在我的JS脚本中,我使用HTML5 Geolocation API获取以度(纬度和经度)表示的用户位置,然后尝试将这些值分配给表单中的两个隐藏字段,然后再将其发送到服务器。

The problem is I use getElementById().value method to assign these value and it does not seem to work! 问题是我使用getElementById().value方法来分配这些值,它似乎不起作用! I can print out the values in the same place in script using a simple alert() message so I'm pretty sure it's getElementById().value that doesn't work. 我可以使用简单的alert()消息在脚本中的相同位置打印出这些值,因此我很确定它的getElementById().value不起作用。

Here's code for my form: 这是我的表单的代码:

<form  id="locationForm" method="post" action="" onsubmit="return getLocation()">
Search for cinema showings in my area<br />
<input type="hidden" name="latitude" id="latitude" value="" />
<input type="hidden" name="longitude" id="longitude" value=""/>
<input type="submit" value="Submit" class="submit"/>
</form>

And here's code for my script: 这是我的脚本的代码:

<script>
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    }
    else {
        alert("Geolocation is not supported by this browser.");

    }
}
function showPosition(position) {

    var lat = position.coords.latitude;
    alert("lat is: " + lat);

    document.getElementById('latitude').value = position.coords.latitude;

    document.getElementById("longitude").value = position.coords.longitude;
}

I apologize for my English ... my native language is Portuguese / Brazil. 我为我的英语致歉...我的母语是葡萄牙语/巴西。

I have the same problem. 我也有同样的问题。 I had to check my codes in many browsers and Windows Versions. 我不得不在许多浏览器和Windows版本中检查代码。

document . 文件。 GetElementById (HIDDENFormVariable) . GetElementById(HIDDENFormVariable)。 value = 'string' does NOT run in ... value ='string'不在...中运行

  • IE 11 Windows 7: Does not run IE 11 Windows 7: 无法运行
  • Mozila Windows 7: Does not run Mozila Windows 7: 无法运行
  • Chrome 37.0.2062.124 Vista Business: Does not run Chrome 37.0.2062.124 Vista Business: 无法运行
  • Chrome 38.0.2125 Windows 7: Does not run Chrome 38.0.2125 Windows 7: 无法运行
  • Chrome 38.0.2125 Windows XP: Does not run Chrome 38.0.2125 Windows XP: 无法运行
  • IE 9 Windows Vista Business: Does run IE 9 Windows Vista Business:是否运行
  • IE 7 Windows XP: Does run IE 7 Windows XP:可以运行

BUT .. 但是..

In Windows Explorer 11, if you change in [Tools] > [Compatibility View Settings] and include your site at the list it will run Ok in Windows 7 with IE 11. Then is not a bug, but it was changed by IE. 在Windows资源管理器11中,如果在[Tools] > [Compatibility View Settings]更改,并将您的站点包含在列表中,它将在带有IE 11的Windows 7中运行“确定”。这不是错误,而是由IE进行了更改。 Chrome do not accept the change - hidden input in any of Windows or old version that I have checked. Chrome不接受更改-我检查过的Windows或旧版本中的任何隐藏输入。

I SOLVED it from another way 我用另一种方式解决了

Where I had <input type="HIDDEN"> 我在哪里<input type="HIDDEN">

I changed to 我改为

<DIV STYLE="visibility:hidden"> <input type="TEXT"> </DIV>

Text inputs can be changed by javascript and inside the DIV they do not need to be showed on the screen. 文本输入可以通过javascript进行更改,并且在DIV无需将其显示在屏幕上。

The issue is that runs asynchronously. 问题是异步运行。 So, the form submission handler will not wait for the geolocation.getCurrentPosition() method to finish and execute it's success callback - where you are assigning the values to input fields. 因此,表单提交处理程序将不会等待geolocation.getCurrentPosition()方法完成并执行它的成功回调-在此将值分配给输入字段。 Instead it continues execution ie: 而是继续执行,即:

You have specified onsubmit="return getLocation()" in the inline event handler but you're not returning anything - hence the function will return undefined immediately and the <form> is submitted, regardless of the status of geolocation method. 您已onsubmit="return getLocation()"联事件处理程序中指定了onsubmit="return getLocation()" ,但未返回任何内容-因此,无论geolocation方法的状态如何,该函数都会立即返回undefined并提交<form>


You can solve this issue by preventing the form submission using event.preventDefault() and manually submitting the form inside the success callback of getCurrentPosition() as shown below: 您可以通过防止使用event.preventDefault()提交表单,并在getCurrentPosition()的成功回调中手动提交表单来解决此问题,如下所示:

HTML: HTML:

<form id="locationForm" method="post" action="">Search for cinema showings in my area
    <br />
    <input type="text" name="latitude" id="latitude" value="" />
    <input type="text" name="longitude" id="longitude" value="" />
    <input type="submit" value="Submit" class="submit" />
</form>

JS: JS:

var form = document.getElementById("locationForm");
form.addEventListener("submit", getLocation);

function getLocation(event) {
    event.preventDefault(); // prevent the form submission
    if (navigator.geolocation) 
        navigator.geolocation.getCurrentPosition(success, error);
     else {
        alert("Geolocation is not supported by this browser");
};

function success(position) {
    var lat = position.coords.latitude,
        long = position.coords.longitude;

    alert("lat is: " + lat + "long is: " + long);
    document.getElementById("latitude").value = lat;
    document.getElementById("longitude").value = long;
    form.submit(); // successfully set the values, submitting the form
}

function error(){
    alert("Unable to retrieve you location");
}

Demo 演示版

Ok,I solved the problem although it can be considered a bit of a "hack". 好的,我解决了这个问题,尽管可以认为它有点“黑客”。 I created a separate button that call the same getLocation function to change the values of latitude and longitude fields and submit the form using: 我创建了一个单独的按钮,调用相同的getLocation函数来更改纬度和经度字段的值,并使用以下方式提交表单:

document.locationForm.submit();

Then on the load of the page I use jQuery to hide the submit button: 然后在页面加载中,我使用jQuery隐藏了Submit按钮:

<script>
$(document).ready(function(){
$(".submit").hide();
});
</script>

What I noticed is that the old script did change values of the hidden fields after all, just did not post them to the server. 我注意到的是,旧脚本毕竟确实更改了隐藏字段的值,只是没有将它们发布到服务器。 If anyone finds out what caused this to not work is welcome, but for now thanks for all the answers! 如果有人发现导致此问题不起作用的原因,欢迎您,但现在感谢所有回答!

If I'm not mistaken, the onsubmit is fired when the form is already being submitted. 如果我没记错的话,在提交表单时会触发onsubmit Try calling the function through the submit button's onclick 尝试通过提交按钮的onclick调用该函数

OK I tried another approach. 好吧,我尝试了另一种方法。 Add a name to to your form, like name="geoform" 为您的表单添加一个名称,例如name="geoform"

Now, in the showPosition function, change the last lines to: 现在,在showPosition函数中,将最后showPosition行更改为:

document.geoform.latitude.value = position.coords.latitude;
document.geoform.longitude.value = position.coords.longitude;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM