HTML源码如下:<html>
<head>
<meta http-equiv=content-type content="text/html; charset=gb2312">
<title>文件上传</title>
<style type="text/css" media="all">
.TdStyle {
color:#339900;
font-size:18px;
font-weight:bold;
cursor:hand;
}
</style>
<script type="text/javascript">
//设置上传文件数量
var fileCount=4;
function addUploadFile()
{
var oTable = document.getElementById("files");
var oTBody = oTable.tBodies[0];
var oRows = oTBody.rows;
if(oRows.length >= (typeof(fileCount)=="undefined" ? 3:fileCount)) return;
var newFileName = "file"+(oRows.length+1);
var newTr = document.createElement("TR");
var newTd1 = document.createElement("TD");
var newTd2 = document.createElement("TD");
//设置属性
newTd1.onclick=function(){deleCurrent(newFileName);};
newTd1.className = "TdStyle";
//设置内容
newTd1.innerHTML = "-";
newTd2.innerHTML="<input type='file' name='"+newFileName+"'/>";
newTr.appendChild(newTd1);
newTr.appendChild(newTd2);
newTr.setAttribute("id",newFileName);
oTBody.appendChild(newTr);
}
function deleCurrent(fileName)
{
var oTable = document.getElementById("files");
var oTBody = oTable.tBodies[0];
for(var i=0; i<oTBody.rows.length; i++)
{
var TrId = oTBody.rows[i].getAttribute("id")
if(TrId == fileName)
{
oTBody.deleteRow(i);
break;
}
}
}
</script></head><body>
<form name="uploadForm" action="" method="post"
enctype="multipart/form-data">
<table border=1>
<tr>
<td>
<table id="files">
<tbody>
<tr id="file1">
<td onClick="addUploadFile();" class="TdStyle">+</td>
<td><input type="file" name="file1"/></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="上传"/>
</td>
</tr>
</table>
</form>
</body>
</html>
想在提交前用JS检测一下是否所有的上传控件都已经选择了文件
<head>
<meta http-equiv=content-type content="text/html; charset=gb2312">
<title>文件上传</title>
<style type="text/css" media="all">
.TdStyle {
color:#339900;
font-size:18px;
font-weight:bold;
cursor:hand;
}
</style>
<script type="text/javascript">
//设置上传文件数量
var fileCount=4;
function addUploadFile()
{
var oTable = document.getElementById("files");
var oTBody = oTable.tBodies[0];
var oRows = oTBody.rows;
if(oRows.length >= (typeof(fileCount)=="undefined" ? 3:fileCount)) return;
var newFileName = "file"+(oRows.length+1);
var newTr = document.createElement("TR");
var newTd1 = document.createElement("TD");
var newTd2 = document.createElement("TD");
//设置属性
newTd1.onclick=function(){deleCurrent(newFileName);};
newTd1.className = "TdStyle";
//设置内容
newTd1.innerHTML = "-";
newTd2.innerHTML="<input type='file' name='"+newFileName+"'/>";
newTr.appendChild(newTd1);
newTr.appendChild(newTd2);
newTr.setAttribute("id",newFileName);
oTBody.appendChild(newTr);
}
function deleCurrent(fileName)
{
var oTable = document.getElementById("files");
var oTBody = oTable.tBodies[0];
for(var i=0; i<oTBody.rows.length; i++)
{
var TrId = oTBody.rows[i].getAttribute("id")
if(TrId == fileName)
{
oTBody.deleteRow(i);
break;
}
}
}
</script></head><body>
<form name="uploadForm" action="" method="post"
enctype="multipart/form-data">
<table border=1>
<tr>
<td>
<table id="files">
<tbody>
<tr id="file1">
<td onClick="addUploadFile();" class="TdStyle">+</td>
<td><input type="file" name="file1"/></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="上传"/>
</td>
</tr>
</table>
</form>
</body>
</html>
想在提交前用JS检测一下是否所有的上传控件都已经选择了文件
function check()
{
if(document.getElementById("你需要的控件ID").value=="")
{
alert("x值不能为空");
return false;
}
retrun true;
}<form name="uploadForm" action="" method="post"
enctype="multipart/form-data" onsubmit="return check();">
{
}
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页 4</title>
</head><body>
<form name="myform">
<input type=file name=file1>
<input type=file name=file1>
<input type=file name=file1>
<input type=file name=file1>
<input type=button onclick="check()" />
</form>
</body></html>
<script type=text/javascript>
function check(){
for(var i=0;i<document.getElementsByName("file1").length;i++){
if(document.getElementsByName("file1")[i].value==""){
alert(i);
}
}
}
</script>
var t = document.getElementsByTagName("input");
for(var i=0;i <t.length;i++){
if(t[i].type=="file"&&t[i].value==""){
alert(i);
}
}
}
var f = document.forms[0];
f.onsubmit = function(){
var t = document.getElementsByTagName("input");
for(var i=0;i <t.length;i++){
if(t[i].type=="file" && t[i].value==""){
alert("第 " + i + "个文件没有上传!");
return false;
}
}
return true;
}
1、在你原有的JS代码中加入:
function checkform(){
var mm=document.getElementsByTagName("input");
for( var ii=0;ii<mm.length;ii++){
if(mm[ii].type=="file" && mm[ii].value.substr(1,2) != ":\\" ){
alert("在第 "+(ii+1)+" 行中,请正确选择要上传的文件!");
mm[ii].focus();
return false;
break;
}
}
return true;
}
2、把代码中的:
<form name="uploadForm" action="" method="post" enctype="multipart/form-data">改为:
<form name="uploadForm" action="" method="post" enctype="multipart/form-data" onSubmit="return checkform()">