Jquery onchange event for cloned field

2019-09-09 22:38发布

问题:

I figured out how to clone my form rows and append an incriminting number to the end of the ID's for each form field. So I thought with that appended incriminenting number at the end of the ID it would then be easy to use on change event to copy the value of one id to another, but this is not working for the cloned rows!

I am using this to create new form rows:

$('#btnRemove').attr('disabled','disabled');
            $('#btnAdd').click(function() {
                var num     = $('.clonedInput').length;
                var newNum  = new Number(num + 1);

                var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

                                newElem.children('.product').attr('id', 'product' + newNum).attr('name', 'product' + newNum);
                newElem.children('.productid').attr('id', 'productid' + newNum).attr('name', 'productid' + newNum);

                $('#input' + num).after(newElem);
                $('#btnRemove').removeAttr('disabled','disabled');

                if (newNum == 7)
                $('#btnAdd').attr('disabled','disabled');


            });

            $('#btnRemove').on('click', function() {
                $('.clonedInput').last().remove();
                $('#btnAdd').removeAttr('disabled','disabled');
            });

And I am (trying) to use this code to copy the serialized product id .val(value.split('-')[3]) from the product select value to the 'id' field in the form. It only works for the first one though, and does not work on the 'cloned' form fields:

 $("#product").on('change keyup', function() {
        var value = $('option:selected', this).val();
            $("#productid").val(value.split('-')[3]);
        }).keyup();
        $("#product2").on('change keyup', function() {
        var value = $('option:selected', this).val();
            $("#productid2").val(value.split('-')[3]);
        }).keyup();
        $("#product3").on('change keyup', function() {
        var value = $('option:selected', this).val();
            $("#productid3").val(value.split('-')[3]);
        }).keyup();
        $("#product4").on('change keyup', function() {
        var value = $('option:selected', this).val();
            $("#productid4").val(value.split('-')[3]);
        }).keyup();
        $("#product5").on('change keyup', function() {
        var value = $('option:selected', this).val();
            $("#productid5").val(value.split('-')[3]);
        }).keyup();
        $("#product6").on('change keyup', function() {
        var value = $('option:selected', this).val();
            $("#productid6").val(value.split('-')[3]);
        }).keyup();​

I put this on jsfiddle so it will be easier to see what my problem is: Demo

回答1:

As you're creating select using clone and append to DOM dynamically, so you need delegate event handler for them

// delegate event handler
$('#myForm').on('change keyup', 'select[id^=product]', function() {
    var value = $('option:selected', this).val();
    $(this).next('input.productid').val(value.split('-')[3]);
}).keyup();

DEMO