jQuery append() and remove() element

后端 未结 2 697
抹茶落季
抹茶落季 2020-12-23 13:44

I have a form where I\'m dynamically adding the ability to upload files with the append function but I would also like to be able to remove unused fields. Here is the html m

相关标签:
2条回答
  • 2020-12-23 14:21

    Since this is an open-ended question, I will just give you an idea of how I would go about implementing something like this myself.

    <span class="inputname">
        Project Images:
        <a href="#" class="add_project_file">
            <img src="images/add_small.gif" border="0" />
        </a>
    </span>
    
    <ul class="project_images">
        <li><input name="upload_project_images[]" type="file" /></li>
    </ul>
    

    Wrapping the file inputs inside li elements allows to easily remove the parent of our 'remove' links when clicked. The jQuery to do so is close to what you have already:

    // Add new input with associated 'remove' link when 'add' button is clicked.
    $('.add_project_file').click(function(e) {
        e.preventDefault();
    
        $(".project_images").append(
            '<li>'
          + '<input name="upload_project_images[]" type="file" class="new_project_image" /> '
          + '<a href="#" class="remove_project_file" border="2"><img src="images/delete.gif" /></a>'
          + '</li>');
    });
    
    // Remove parent of 'remove' link when link is clicked.
    $('.project_images').on('click', '.remove_project_file', function(e) {
        e.preventDefault();
    
        $(this).parent().remove();
    });
    
    0 讨论(0)
  • 2020-12-23 14:34

    You can call a reset function before appending. Something like this:

        function resetNewReviewBoardForm() {
        $("#Description").val('');
        $("#PersonName").text('');
        $("#members").empty(); //this one what worked in my case
        $("#EmailNotification").val('False');
    }
    
    0 讨论(0)
提交回复
热议问题