Can't get value of input type=“file”?

后端 未结 8 2041
谎友^
谎友^ 2020-11-27 16:48

I have a

When I\'m using: alert($(\"#uploadPicture\").val());

It ale

相关标签:
8条回答
  • 2020-11-27 16:59
    $('input[type=file]').val()
    

    That'll get you the file selected.

    However, you can't set the value yourself.

    0 讨论(0)
  • 2020-11-27 17:01

    don't give this in file input value="123".

      $(document).ready(function(){  
    
          var img = $('#uploadPicture').val();
    
      });
    
    0 讨论(0)
  • 2020-11-27 17:03

    Solution

    document.getElementById("uploadPicture").attributes.value.textContent
    
    0 讨论(0)
  • 2020-11-27 17:04

    You can't set the value of a file input in the markup, like you did with value="123".

    This example shows that it really works: http://jsfiddle.net/marcosfromero/7bUba/

    0 讨论(0)
  • 2020-11-27 17:04

    It's old question but just in case someone bump on this tread...

    var input = document.getElementById("your_input");
    var file = input.value.split("\\");
    var fileName = file[file.length-1];
    

    No need for regex, jQuery....

    0 讨论(0)
  • 2020-11-27 17:05

    @BozidarS: FileAPI is supported quite well nowadays and provides a number of useful options.

    var file = document.forms['formName']['inputName'].files[0];
    //file.name == "photo.png"
    //file.type == "image/png"
    //file.size == 300821
    
    0 讨论(0)
提交回复
热议问题