Javascript功能在谷歌浏览器中无效

时间:2014-05-23 09:15:21

标签: javascript jquery html struts

附件是我的代码。当我点击'上传更多文件'时,addElement功能将添加一个浏览文件。它在IE中工作,但它无法在Google Chrome中运行。在Chrome中,当我点击按钮时,它甚至没有响应。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<script>

var fCount = 1;
function addElement() {

fCount++;

var fObject = document.getElementById('fileSection');
var text = 'File:';
var tag='<input type="file" name="theFile[' +  fCount +  ']" value="">';
var brk='<br>'
var o1 = document.createTextNode(text);
var o2 = document.createElement(tag);
var o3 = document.createElement(brk);

   fObject.appendChild(o3);
   fObject.appendChild(o1);
   fObject.appendChild(o2);
   fObject.appendChild(o3);

  alert("fCount" + fCount);

}

</script>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="pink">

<html:form action="/myActionForm" method="post"
    enctype="multipart/form-data">

            <div id="fileSection">
            Select file to upload <html:file property="theFile[0]"/><br>
            </div>
        <html:button property="bt" onclick="addElement()">Upload More files</html:button>


    <html:submit></html:submit>
    <br>
    <br>

</html:form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

这是因为您需要将tag name传递给createElement方法。不是元素片段。 IE总是很奇怪,可能是工作,但这是错误的。

正确的方法是这样的:

var tag = document.createElement('input');
tag.setAttribute('type', 'file');
tag.setAttribute('name', 'theFile[' +  fCount +  ']');