简体   繁体   English

使用带有“ split”的jQuery警报输入“文件”文件名

[英]Alert input “file” filename using jQuery with “split”

I'm trying to alert the filename from a upload input. 我正在尝试从上传输入中提醒文件名。

Here's my fiddle 这是我的小提琴

It works, but there's the "C:Fakepath..." something like that. 它可以工作,但是有类似的“ C:Fakepath ...”。 I just want the filename, without the Fake path. 我只想要文件名,没有伪路径。 I tried using the split function, but I'm not sure why it's not working. 我尝试使用split函数,但不确定为什么它不起作用。

Example html: 范例html:

<html>
    <head>
        <title>Test Page</title>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
        <input type="file" id="tester" />
    </body>
</html>

Example script: 示例脚本:

$(function() {
    var bogus;
    var triple;
    $('#tester').change(function() {

        triple = $('#tester').val();
        bogus = triple.split(/[\s/]+/);

        alert(bogus[bogus.length - 1]);
    });
});

​ Any idea? 有什么想法吗?

Thank you! 谢谢!

You can just escape the slash: 您可以逃脱斜线:

   bogus = triple.split("\\");

Updated fiddle: http://jsfiddle.net/johnkoer/xwdct/3/ 更新的小提琴: http : //jsfiddle.net/johnkoer/xwdct/3/

If your web browser supports the File API (currently, this is any web browser that isn't IE), you can get the file name from the input's FileList object. 如果您的Web浏览器支持File API (当前是不是IE的任何Web浏览器),则可以从输入的FileList对象获取文件名。

$(function() {
    $('#tester').change(function() {
        var files = this.files;
        if (files && files.length) {
            alert(files[0].name); //use the file api
        }
        else {
            alert($(this).val().replace("C:\\fakepath\\", ""));  //this is for IE
        }
    });
});​

Example: http://jsfiddle.net/NTICompass/xwdct/6/ 示例: http//jsfiddle.net/NTICompass/xwdct/6/

Docs: https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications 文件: https//developer.mozilla.org/en-US/docs/Using_files_from_web_applications

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 jQuery/JavaScript 在动态输入文件字段中获取选定的文件名 - Get Selected Filename in dynamic input file fields using jQuery/JavaScript 如果输入类型文件不是图像,则使用 jquery 脚本显示警报 - show alert if input type file is not image ,using jquery script 使用jquery的自定义文件输入会限制用户文件类型和文件名,如果用户单击复选框则会禁用文件输入 - Custom file input using jquery restrict the user file types and filename if the user click check box it input file disable 如何使用JavaScript或JQuery从input type = file html元素获取文件名? - How to get the filename from input type=file html element using JavaScript or JQuery? 输入类型文件OnChange无法在jquery中获取文件名? - input type file OnChange not able to get filename in jquery? 使用 jQuery 获取文件输入的选定文件名而不带路径 - Use jQuery to get the file input's selected filename without the path 如何使用jQuery拆分输入值? - How to split input value using jquery? Bootstrap 4 / jQuery - 在输入字段中清除文件名<input type="file"> bs-自定义文件输入 - Bootstrap 4 / jQuery - Clear filename in input field <input type='file' /> bs-custom-file-input 如何以方形空间形式提醒上传文件的文件名? - how to alert filename of uploaded file in squarespace form? 当/如果输入值实时更改使用jQuery时,如何显示警报? - How to show alert when / if input value live change using jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM