简体   繁体   English

IE7抱怨JavaScript中的标签ID

[英]IE7 complaining about label ID in javascript

Ok, I am developing a simple program to do xmlhttprequests so I can get a handle on how they work. 好的,我正在开发一个简单的程序来执行xmlhttprequests,这样我就可以了解它们的工作方式。 On that page I have a simple text box, a label, and a button. 在该页面上,我有一个简单的文本框,一个标签和一个按钮。 When the button is clicked it sends out a request to another page via the javascript method, and it stores the response in the label. 单击该按钮时,它会通过javascript方法将请求发送到另一个页面,并将响应存储在标签中。

(this is all the code in the body) (这是体内的所有代码)

<form id="form1" runat="server">
        <div>
            <input type="text" id="text1" value="StuffInTheBox" name="text1"/>
            <label id="label1">Please Enter Name.</label>
        </div>
</form>
<button id="button1" onclick="checkName(text1.value,'')">BUTTON</button>

This works perfectly in google chrome. 这在谷歌浏览器中完美运行。 But when it came time to try it out in IE7 it gave me an error. 但是当需要在IE7中进行尝试时,它给了我一个错误。 It said "Error: 'text1' is undefined". 它说:“错误:'text1'未定义”。 I've been trying to tweak everything I can to see if it makes a difference but now I'm kind of lost. 我一直在尝试调整所有可以查看的内容是否有所帮助,但现在我有点迷失了。

Any help would be much appreciated 任何帮助将非常感激

edit: checkname function per request 编辑:每个请求的检查名称功能

The method calls loadXMLDoc which creates the xmlhttprequest object, forking the construction for older IE who use ActiveX and modern browers who have it native. 该方法调用loadXMLDoc来创建xmlhttprequest对象,从而为使用ActiveX的旧版IE和具有本机的现代浏览器进行构建。 It also creates a method to watch the status change, and if it is done successfully it recalls checkname with checkName('',results) 它还创建了一个监视状态变化的方法,如果成功完成,它将使用checkName('',results)调用checkname。

function checkName(input, response)
    {        
      if (response != ''){ 
        // Response mode
        message   = document.getElementById('label1');
        message.innerHTML = response;

      }else{
        // Input mode
        loadXMLDoc("http://localhost/xmlTest/Return.aspx","input="+input);
      }
    }

In your JavaScript "checkName(text1.value,'')" it is unclear what text1.value is referencing. 在您的JavaScript“ checkName(text1.value,'')”中,尚不清楚正在引用什么text1.value。 You're assuming it's referencing the DOM Object you've declared in your HTML and FireFox seems to make that assertion as well however IE doesn't. 您假设它引用的是您在HTML中声明的DOM对象,而FireFox似乎也做出了该声明,而IE没有。 text1 could have easily been a reference to an object declared earlier in your JavaScript code. text1可能很容易成为对JavaScript代码中先前声明的对象的引用。

var text1 = {value: ""};

Frankly I'm surprised that FireFox didn't throw an error. 坦白说,我很惊讶FireFox没有抛出错误。

When referring to DOM objects (ie HTML elements) you need to use the document.getElementById or document.getElementsByName methods. 当引用DOM对象(即HTML元素)时,您需要使用document.getElementByIddocument.getElementsByName方法。

The following example was tested and works in both FireFox and IE and I would assume to work in Chrome, Safari and Opera as well. 以下示例已经过测试,并且可以在FireFox和IE中使用,并且我假设也可以在Chrome,Safari和Opera中使用。

<form id="form1" runat="server">
        <div>
            <input type="text" id="text1" value="StuffInTheBox" name="text1"/>
            <label id="label1">Please Enter Name.</label>
        </div>
</form>
<button id="button1" onclick="checkName(document.getElementById('text1').value,'')">BUTTON</button>

"text1" is the id of the input, but you haven't declared that the text1 variable in the JavaScript refers to that. “ text1”是输入的ID,但您尚未声明JavaScript中的text1变量引用了该变量。

Perhaps this will work for you: 也许这对您有用:

<button id="button1" onclick="checkName(document.getElementById('text1').value,'')">BUTTON</button>

It uses document.getElementById to retrieve the input before trying to find its value. 在尝试查找输入值之前,它使用document.getElementById检索输入。

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

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