是否可以<input type='file' />
使用jQuery 清除控件值?我尝试过以下方法:
$('#control').attr({ value: '' });
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
我试图用它简单而优雅的API来接受jQuery 100%,但我遇到了API和直接HTML之间的不一致,我无法弄清楚.
我有一个AJAX文件上传器脚本(它正常运行),我希望每次文件输入值更改时运行.这是我的工作代码:
<input type="file" size="45" name="imageFile" id="imageFile" onchange="uploadFile()">
Run Code Online (Sandbox Code Playgroud)
当我将onchange
事件转换为jQuery实现时:
$('#imageFile').change(function(){ uploadFile(); });
Run Code Online (Sandbox Code Playgroud)
结果不一样.使用该onchange
属性,uploadFile()
只要按预期更改值,就会调用该函数.但是使用jQuery API .change()
事件处理程序,事件仅在第一次更改值时触发.忽略之后的任何值更改.这对我来说似乎不对,但肯定这不是jQuery的疏忽,对吧?
有没有其他人遇到过同样的问题,你有解决方法或解决方案,除了我上面描述的问题?
回覆:
上述问题涉及在进行文件选择后,使"更改"事件在浏览器中一致地触发.http://jsfiddle.net/7wR2L/上的示例证明了这一点已得到解决
我的情况有点不同.看来这个问题在另一个背景下变得丑陋.
基于设计约束,我必须使用非文件输入元素('a'标签)来触发文件输入元素上的"click"事件.到目前为止,我的测试看起来,当以这种方式选择文件时,文件输入无法触发"更改"通知.
请查看http://jsfiddle.net/rudylattae/7wR2L/8/上的示例
测试环境:
Windows Server 2008 R2
Windows XP专业版(2002 - SP3)
I have html:
<form id="fileuploadform">
<input type="file" id="fileupload" name="fileupload" />
</form>
Run Code Online (Sandbox Code Playgroud)
and jquery:
$(':file').change(function(){
var file = this.files[0];
...
});
Run Code Online (Sandbox Code Playgroud)
The problem is: the change function fires only when the file selection is different than the selection made previously. That is of course the meaning of the 'change' event. I want my function to be called with the file dialog closes, no matter what.
我尝试使用 'select' 事件——它永远不会被调用
我也试过:
$(':file').bind('dialogclose', function(event) {
alert('closed');
});
Run Code Online (Sandbox Code Playgroud)
和:
$(':file').live("dialogclose", function(){
alert('closed1');
});
Run Code Online (Sandbox Code Playgroud)
他们也没有工作。
jquery ×4
file-io ×2
html ×2
javascript ×2
events ×1
file-upload ×1
forms ×1
input ×1
onchange ×1