jQuery-检测文件输入中是否已选择文件 [英] jQuery - Detecting if a file has been selected in the file input

查看:69
本文介绍了jQuery-检测文件输入中是否已选择文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
jQuery:获取从< input type中选择的文件名=文件" />

Possible Duplicate:
jQuery: get the file name selected from <input type=“file” />

我有一个标准的文件输入框

I have a standard file input box

<input type="file" name="imafile">

我在页面下方也有一些文字

I also have a bit of text down the page like so

<span class="filename">Nothing selected</span>

我想知道是否可以使用在文件输入框中选择的文件名来更新文本?

I was wondering if it is possible to have the text update with the name of the file selected in the file input box?

干杯

推荐答案

您应该能够将事件处理程序附加到输入的onchange事件上,并具有调用函数以在您的跨度中设置文本的功能.

You should be able to attach an event handler to the onchange event of the input and have that call a function to set the text in your span.

<script type="text/javascript">
  $(function() {
     $("input:file").change(function (){
       var fileName = $(this).val();
       $(".filename").html(fileName);
     });
  });
</script>

您可能希望将ID添加到您的输入和跨度中,以便您可以基于特定于您所关注元素的ID进行选择,而不是DOM中的其他文件输入或跨度.

You may want to add IDs to your input and span so you can select based on those to be specific to the elements you are concerned with and not other file inputs or spans in the DOM.

这篇关于jQuery-检测文件输入中是否已选择文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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