我遇到的问题是我的代码在 JavaScript 中工作正常,但在 Firefox 或 safari 中无法正常工作,想知道为什么。我正在做的是,我有一个循环遍历每个元素,并根据文本框中的变量只想发出警报。正如我之前所说,这段代码在 IE 中运行良好。下面是代码:
以下是文本框的示例:
<asp:TextBox ID="txtMac" runat="server" req="yes" errMessage="Mac"/>
for (a = 0; a < theForm.elements.length; a++) {
if (theForm.elements[a].type == "text" && theForm.elements[a].req == "yes") {
alert("Made it here")
}
}
最佳答案
使用getAttribute读取自定义属性。请参阅http://jsfiddle.net/8EWQr/ .
所以而不是
(theForm.elements[a].type == "text"&& theForm.elements[a].req == "yes")
使用
(theForm.elements[a].getAttribute('type') == "text"&& theForm.elements[a].getAttribute('req') == "yes")
关于处理不同浏览器的JavaScript问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7165804/