简体   繁体   中英

jquery delete dynamically created input field

So im creating project in which you can create unlimited number of input fields that belong to the same array and eventualy are being posted to php handler via ajax. i managed to get that far, its working all fine but the problem im having is that i would want to let user to delete input he/she doesnt want (ie created by mistake), it seems to be core part of script, yet i dont know how to approach the issue.

This project you can see in here:

http://jsfiddle.net/7LCzN/

and this is the code:

$(function(){
    $("#add").on('click', function () {
        $('body').append('<input type="text" class="ac" name="array[]" />');
    });
});

$(function(){
    $("#post").on('click', function () {

        var myArray = new Array();

        $('.ac').each(function(index, elem) {
            myArray.push($(elem).val());
        });
        $('#result').text(myArray);
    });
});

So for instance ive created 4 fields with these value:

5463, 8675, 2340, 1203

and i just realized i dont want the one with value 2340 i would want to delete it so im left with 3 fields:

5463, 8675, 1203

anyone that helps, ill be glad and greatful, thank you fellows:)

.remove() is a jQuery function that you can use to remove elements from the DOM.

Here's a tiny example:

$(".inputToRemove").remove();

Here's a fork of your jsFiddle for a working example.

From the docs:

Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on(). To ensure the elements are present and can be selected, perform event binding inside a document ready handler for elements that are in the HTML markup on the page. If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page. Or, use delegated events to attach an event handler, as described next.

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

In addition to their ability to handle events on descendant elements not yet created, another advantage of delegated events is their potential for much lower overhead when many elements must be monitored. On a data table with 1,000 rows in its tbody, this example attaches a handler to 1,000 elements:

$( "#dataTable tbody tr" ).on( "click", function() {
alert( $( this ).text() );
});

A delegated-events approach attaches an event handler to only one element, the tbody, and the event only needs to bubble up one level (from the clicked tr to tbody):

$( "#dataTable tbody" ).on( "click", "tr", function() {
alert( $( this ).text() );
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM