此代码应该在IE中工作(甚至不在Firefox中测试),但事实并非如此.我想要的是显示附件的名称.有帮助吗?
<html>
<head>
<title>example</title>
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript">
$(document).ready( function(){
$("#attach").after("<input id='fakeAttach' type='button' value='attach a file' />");
$("#fakeAttach").click(function() {
$("#attach").click();
$("#maxSize").after("<div id='temporary'><span id='attachedFile'></span><input id='remove' type='button' value='remove' /></div>");
$('#attach').change(function(){
$("#fakeAttach").attr("disabled","disabled");
$("#attachedFile").html($(this).val());
});
$("#remove").click(function(e){
e.preventDefault();
$("#attach").replaceWith($("#attach").clone());
$("#fakeAttach").attr("disabled","");
$("#temporary").remove();
});
})
});
</script>
</head>
<body>
<input id="attach" type="file" /><span id="maxSize">(less than 1MB)</span>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Thi*_*ker 146
就像写作一样简单:
$('input[type=file]').val()
Run Code Online (Sandbox Code Playgroud)
无论如何,我建议使用名称或ID属性来选择您的输入.对于事件,它应该如下所示:
$('input[type=file]').change(function(e){
$in=$(this);
$in.next().html($in.val());
});
Run Code Online (Sandbox Code Playgroud)
Ana*_*hyn 19
$('input[type=file]').change(function(e){
$(this).parents('.parent-selector').find('.element-to-paste-filename').text(e.target.files[0].name);
});
Run Code Online (Sandbox Code Playgroud)
如果使用,此代码将不会显示C:\fakepath\在Google Chrome中的文件名之前.val().
Jam*_*111 19
最简单的方法是简单地使用以下行jquery,使用这个你没有得到/fakepath废话,你直接得到上传的文件:
$('input[type=file]')[0].files[0]; // This gets the file
$('#idOfFileUpload')[0].files[0]; // This gets the file with the specified id
Run Code Online (Sandbox Code Playgroud)
其他一些有用的命令是:
要获取文件的名称:
$('input[type=file]')[0].files[0].name; // This gets the file name
Run Code Online (Sandbox Code Playgroud)
要获取文件的类型:
如果我要上传PNG,它会返回 image/png
$("#imgUpload")[0].files[0].type
Run Code Online (Sandbox Code Playgroud)
要获取文件的大小(以字节为单位):
$("#imgUpload")[0].files[0].size
Run Code Online (Sandbox Code Playgroud)
此外,您不必使用这些命令on('change',您可以随时获取值,例如,您可能有文件上载,当用户单击时upload,您只需使用我列出的命令.
| 归档时间: |
|
| 查看次数: |
193163 次 |
| 最近记录: |