HTML <input type='file'> File Selection Event

后端 未结 5 2155
一生所求
一生所求 2020-12-22 22:12

Let\'s say we have this code:

相关标签:
5条回答
  • 2020-12-22 22:35

    When you have to reload the file, you can erase the value of input. Next time you add a file, 'on change' event will trigger.

    document.getElementById('my_input').value = null;
    // ^ that just erase the file path but do the trick
    
    0 讨论(0)
  • 2020-12-22 22:42

    Listen to the change event.

    input.onchange = function(e) { 
      ..
    };
    
    0 讨论(0)
  • 2020-12-22 22:45

    That's the way I did it with pure JS:

    var files = document.getElementById('filePoster');
    var submit = document.getElementById('submitFiles');
    var warning = document.getElementById('warning');
    files.addEventListener("change", function () {
      if (files.files.length > 10) {
        submit.disabled = true;
        warning.classList += "warn"
        return;
      }
      submit.disabled = false;
    });
    #warning {
        text-align: center;
    }
    
    #warning.warn {
    	color: red;
    	transform: scale(1.5);
    	transition: 1s all;
    }
    <section id="shortcode-5" class="shortcode-5 pb-50">
        <p id="warning">Please do not upload more than 10 images at once.</p>
        <form class="imagePoster" enctype="multipart/form-data" action="/gallery/imagePoster" method="post">
            <div class="input-group">
      	    <input id="filePoster" type="file" class="form-control" name="photo" required="required" multiple="multiple" />
    	    <button id="submitFiles" class="btn btn-primary" type="submit" name="button">Submit</button>
            </div>
        </form>
    </section>

    0 讨论(0)
  • 2020-12-22 22:46

    jQuery way:

    $('input[name=myInputName]').change(function(ev) {
    
        // your code
    });
    
    0 讨论(0)
  • 2020-12-22 22:51

    The Change event gets called even if you click on cancel..

    0 讨论(0)
提交回复
热议问题