Adding file inputs dynamically with jquery?

后端 未结 4 460
萌比男神i
萌比男神i 2021-01-20 00:29

To make my weppage as compatible as possible I will go with the regular file input, the problem is that I need to offer multiple uploads.

My thought is that when the

相关标签:
4条回答
  • 2021-01-20 00:35

    You could have a container div which will harbor the new file input fields and a button to add new inputs:

    $('#addFile').click(function() {
        // when the add file button is clicked append
        // a new <input type="file" name="someName" />
        // to a filesContainer div
        $('#filesContainer').append(
            $('<input/>').attr('type', 'file').attr('name', 'someName')
        );
    });
    
    0 讨论(0)
  • 2021-01-20 00:37

    Another option, since you are using JQuery is the Bootstrap fileInput. It lets you upload multiple images with one control. You have additional options too like the allowed file types, size, height, width, etc.

    <script type="text/javascript">
        $("#postedImage").fileinput({
          uploadUrl: "/SomeUrl", // you must set this for ajax uploads
          maxFileCount: 10,
          enctype: "multipart/form-data",
          overwriteInitial: false
        });
    </script>
    
    <input id="postedImage" type="file" class="file" multiple>
    

    Here is a link for other uploaders if you are interested.

    0 讨论(0)
  • 2021-01-20 00:47

    Yes, you can just add a file input to the form as you would any other element:

    $("#theForm").append("<input type='file' name='foo'>");
    

    I thought this sounded familiar: [jquery]How do I add file uploads dynamically?

    0 讨论(0)
  • 2021-01-20 00:49

    if you want to have diffrent input names

    var i;
    $('#addFile').click(function() {
        i=i+1;
        $('#filesContainer').append(
            $('<input/>').attr('type', 'file').attr('name', 'file'+i)
        );
    });
    
    0 讨论(0)
提交回复
热议问题