AJAX POST发送多个DIV值

问题描述 投票:0回答:1

我想发送动态生成的DIV项目,但我不明白该怎么做

我动态创建我的div:

$("#clt_forf_ti").click(function(e){
            e.preventDefault();
            if(x < max_input){
                x++;
                var addDIV = $('<div id="clt_forf_div'+x+'" class="w-100 form-control" ><label for="clt_forf_num'+x+'">Ligne :</label> <input type="text" class="font-weight-bold border-top-0 border-right-0 border-left-0 mR-10" id="clt_forf_num'+x+'" autocomplete="off" style="width: 100px" > Forfait : <i id="clt_forf_forf_select'+x+'" style="display:none;"></i><input type="text" class="border-top-0 border-right-0 border-left-0" id="clt_forf_forf'+x+'" autocomplete="off" style="width: 180px"><i class="ti-calendar mL-20 mR-5"></i><input type="text" class="border-top-0 border-right-0 border-left-0" id="clt_forf_date_start'+x+'" name="datepicker" placeholder="Début" style="width: 80px"><i class="ti-calendar mL-20 mR-5"></i><input type="text" class="border-top-0 border-right-0 border-left-0" id="clt_forf_date_end'+x+'" name="datepicker" placeholder="Fin" style="width: 80px"><i id="clt_forf_ti_remove" class="ti-close c-red-500 mL-5" style="cursor: pointer;"></i></div>');

                $('#clt_forf_bloc').append(addDIV);
                $('#clt_forf_forf'+x, addDIV).autocomplete(autocomp_opt);
                $("input[id^='clt_forf_date']", addDIV).datepicker(datepick);
                //$('#clt_forf_date_end'+x, addDIV).datepicker(datepick);
            }
        });

我想通过ajax发送每个值:

var data = {
                op_clt_forf_ref: $("[id^='clt_forf_forf_select']").html(),
                op_clt_forf_num: $("[id^='clt_forf_num']").val(),
                op_clt_forf_start: $("[id^='clt_forf_date_start']").val(),
                op_clt_forf_end: $("[id^='clt_forf_date_end']").val(),
                op_clt_forf_nom: $("[id^='clt_forf_forf']").val(),
            };

但他只带我第一个div而不是其他人,

你能帮助我吗 ?

ajax post multiple-value
1个回答
0
投票

您之前创建的元素不存在于DOM中。所以不能直接针对他们。试着这样做:

var data = {
            op_clt_forf_ref: $('body #clt_forf_forf_select').html(),
            op_clt_forf_num: $('body #clt_forf_num').val(),
            op_clt_forf_start: $('body #clt_forf_date_start').val(),
            op_clt_forf_end: $('body #clt_forf_date_end').val(),
            op_clt_forf_nom: $('body #clt_forf_forf').val(),
        };
© www.soinside.com 2019 - 2024. All rights reserved.