jsp <input type="file"> 清空value值的一种方式

浏览器出于安全考虑,不能给jsp<input type='file'/>的value赋值。

我们可以使用javascript来重写对应的<input type='file'/>即可。

==========================================javascript========================================================

function uploadFile(btn,obj){
var content = $(btn).val(); 
if(content == "修改"){
$(btn).attr("value","取消");
}else{
$(btn).attr("value","修改");
switch(obj){//重置<input type='file'>
case "photo":
$("#photo").html("");
var newHtml = '<input type="file" name="photoPic" id="photoPic" style="160px;">';
$("#photo").html(newHtml);
break;
case "card":
$("#card").html("");
var newHtml = '<input type="file" name="cardPic" id="cardPic" style="160px;">';
$("#card").html(newHtml);
break;
default:
alert("您选择上传的图片取消失败,请点击返回按钮后重新修改");
}
}
$("#"+obj).toggle();
}

==========================================jsp========================================================

<td class="tdd">

   <input id="photoBtn"  type="button" value="修改" onclick="uploadFile(this,'photo');">
</td>
<td>
<table width="100%">
<tr>
<td class="tdd" id="photo" style="display:none">
<input type="file" name="photoPic" id="photoPic" style="160px;">
</td>
<td class="tdddd" width="60%" style="color: red">
<label id="photoPicMsg"></label>
</td>
</tr>
</table>
</td>

原文地址:https://www.cnblogs.com/yjtx/p/4429628.html