此代码应该在 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>
最佳答案
写起来就这么简单:
$('input[type=file]').val()
无论如何,我建议使用名称或 ID 属性来选择您的输入。 对于事件,它应该是这样的:
$('input[type=file]').change(function(e){
$in=$(this);
$in.next().html($in.val());
});
关于jQuery:获取从 <input type ="file"/> 中选择的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1299052/