中选择的文件名-6ren"> 中选择的文件名-此代码应该在 IE 中 可以工作(甚至不要在 Firefox 中测试它),但事实并非如此。我想要的是显示附件的名称。有帮助吗? example $(docu-6ren">
gpt4 book ai didi

jQuery:获取从 <input type ="file"/> 中选择的文件名

转载 作者:IT王子 更新时间:2023-10-29 03:25:28 29 4
gpt4 key购买 nike

此代码应该在 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:获取从 &lt;input type ="file"/> 中选择的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1299052/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com