思路是在表單提前時(shí),如果文件控件沒有選擇文件,則自動(dòng)刪除這些控件,再提交表單。但是在實(shí)際操作中報(bào)錯(cuò),不知道是什么原因,煩請(qǐng)指教改正,謝謝。完整代碼如下,瀏覽器第21行報(bào)錯(cuò),已經(jīng)注釋出來(lái)。<!DOCTYPE?html>
<html>
????<head>
<meta?charset="utf-8"?/>
????????<title>首頁(yè)</title>
<style>
</style>
????</head>
????<body>
<form?method="POST"?enctype="multipart/form-data">
<input?type="file"?name="file[]"?/>
<input?type="file"?name="file[]"?/>
<input?type="file"?name="file[]"?/>
<input?type="submit"?/>
</form>
<script>
document.getElementsByTagName('form')[0].addEventListener(
'submit',
function(e){
for(var?inputs=document.getElementsByName('file[]'),?i=0,?l=inputs.length;?i<l;?i++){
try{
if(inputs[i].files.length===0){?//Cannot?read?property?'files'?of?undefined.
inputs[i].parentNode.removeChild(inputs[i]);
}
}catch(ex){
alert(ex.message);
}
}
e.preventDefault();
},
false
);
</script>
<?php
echo?'<pre>';
print_r($_FILES);
echo?'</pre>';
?>
????</body>
</html>
JS 動(dòng)態(tài)刪除表單項(xiàng)時(shí)出現(xiàn)的問(wèn)題
丟矢矣
2015-03-18 21:38:06