简体   繁体   English

JavaScript无法在IE10中使用

[英]Javascript not working in IE10

The script below works okay in Windows XP IE8, but when I tried in Windows 7 IE10, it does not work. 下面的脚本在Windows XP IE8中可以正常运行,但是当我在Windows 7 IE10中尝试时,它无法正常工作。 Please advise. 请指教。 I need to get the form names and values. 我需要获取表单名称和值。 It works if I run it under Compatibility View. 如果我在“兼容性视图”下运行它,它将起作用。 Please advise. 请指教。

function test1()
{
var x = document.getElementById("myForm");
for (var i=0; i<x.length; i++)
   {
  alert(x.elements[i].name + " : " + x.elements[i].value);
  }
}

You need to loop on the elements , not on the form itself. 您需要在elements上循环,而不是在表单本身上循环。

Try this out: 试试看:

function test1() {
  var form = document.getElementById("myForm");
  var formElements = form.elements;

  for (var i=0; i < formElements.length; i++) {
    var field = formElements[i];
    alert(field.name + " : " + field.value);
  }
}

Also, be sure that your form really has the myForm ID, or it won't be found by the script. 另外,请确保您的表单确实具有myForm ID,否则脚本将找不到该ID。

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

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