欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

JS如何获取input中file的值并显示在页面

程序员文章站 2022-04-22 22:27:21
...
本文主要为大家分享一篇JS获取input[file]的值并显示在页面的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

实例如下所示:

$(document).on('change', '.photo-box .file', function () {
  //alert($(this).val());
  function getObjectURL(file) {
    var url = null;
    if (window.createObjectURL != undefined) { // basic
      url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
      url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
      url = window.webkitURL.createObjectURL(file);
    }
    return url;
  }
  var objUrl = getObjectURL(this.files[0]);
  console.log("objUrl = " + objUrl);
  var html = '<p class="photo-box"><img src="' + objUrl + '" alt=""><p class="photo-btn"><p>删除</p></p></p>';
  $(this).parent().parent().append(html);
})

相关推荐:

js清空input file上传文件的内容代码

HTML5之type=file文件上传功能

如何清空input中[type="file"]的值

以上就是JS如何获取input中file的值并显示在页面的详细内容,更多请关注其它相关文章!