如何使用JavaScript来进行文件上传字段为空在Struts2?(How to make a f

2019-09-27 23:42发布

我有一个文件上传字段,即, <s:file> </s:file> 。 我有一个按钮“清除”。 在点击这个按钮,里面有一些文件链接领域应该成为empty.Could谁能帮助我?

我的代码:

<s:file id="filetestplanid2" name="testPlanDto.testFile" label="test"
        tooltipIconPath="../../KY/images/common/buttons/uploadBtn.png"  
        title="Browse" tooltip="Browse..."  cssClass="file" />

我的javascript:

$('#filetestplanid2').val(null);
$('#filetestplanid2').val("");

我尝试了这些,但没有运气。

也尝试:

var file = $("#filetestplanid2"); file.replaceWith(file = file.clone(true));

$('#filetestplanid2').html( $('#filetestplanid2').html() );

Answer 1:

假设

<input type="button" onclick="clearFileElement('filetestplanid2');" />

香草JS

function clearFileElement(fileId){
    document.getElementById(fileId).value = '';
};

演示: http://jsfiddle.net/2nxGr/

不能让工作与jQuery但基本上你有自身的克隆来代替它(与所有的属性,使用获得的clone(true) )。

只要坚持以纯JS版,它就像一个魅力。

编辑

我现在已经发现,在每一个浏览器的工作原理非常聪明的解决方案: https://stackoverflow.com/a/13351234/1654265



Answer 2:

你可以把它清空使用jQuery太多,就像这样:

$("#filetestplanid2")[0].value = ""

要么

$("#filetestplanid2").get(0).value = ""


文章来源: How to make a file upload field empty in struts2 using javascript?