简体   繁体   中英

How to go through array with dynamically added elements with jquery?

I have a HTML table created with javascript where i have possibility to add new rows (dynamically added elements). In one of the tds i have an input field that fires a bootstrap modal to open. In the modal I have radiobuttons with options and when OK button is clicked the value from the radiobutton is set in the inputfield. The problem is that the value gets updated on every row, not just the row i clicked.

Since the elements are added dynamically i used

$(document).on('click', 'input', function (e) {
     doSomething();
});

Any idea how to solve this problem?

Updated with more code

$(document).on('click', 'input', function (e) {

    var inputForm = e.target;
    modal.modal({show:true});
    modal.find(".btn-success").click(function () {
        var choice = $("modal").find('input[type=radio]:checked').val();

        if (choice) {
            modal.modal('hide');
            inputForm.value = choice;
        }
    });
});

Without any furter information about the code you are running it's hard to help you.

But, you might be able to use the target property of the event (e), to look at what element actually triggered the click, to be able to see which row/textbox to update when the modal is closed.

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