如何使用JavaScript在struts2中使文件上传字段为空? [英] How to make a file upload field empty in struts2 using javascript?

查看:78
本文介绍了如何使用JavaScript在struts2中使文件上传字段为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件上传字段,即<s:file> </s:file>.我有一个按钮清除".单击此按钮后,带有某些文件链接的字段应为空.有人可以帮我吗?

I have a file upload field,that is,<s:file> </s:file>. And I have a button "clear". On clicking this button, the field which has some file link should become empty.Could anyone help me with this?

我的代码:

<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("");

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

I tried these but no luck.

也尝试过:

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

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

推荐答案

假设

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

香草JS

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

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

不能使其与jQuery一起使用,但基本上您必须用其自身的克隆替换(具有所有属性,这些属性是使用clone(true)获得的).

Can't make it works with jQuery but basically you have to substitute it with a clone of itself (with all the properties, gained using clone(true)).

只需坚持普通的JS版本,它就像一个魅力.

Just stick to the plain JS version, it works like a charm.

编辑

我现在发现了一种非常聪明的解决方案,可以在每种浏览器上使用: https://stackoverflow.com/a/13351234/1654265

I've found now a very smart solution that works on every browser: https://stackoverflow.com/a/13351234/1654265

这篇关于如何使用JavaScript在struts2中使文件上传字段为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆