简体   繁体   English

POST表单动态生成的输入

[英]POST form dynamically generated inputs

I have a form to which inputs are dynamically added upon click of button that calls an append function. 我有一个窗体,单击调用附加函数的按钮后,动态向其中添加输入。 Once the form is submitted, I only have access to the inputs that were created on page load. 提交表单后,我只能访问页面加载时创建的输入。 I have double checked this fact using the following debug code. 我已使用以下调试代码仔细检查了这一事实。 The alert indeed does not display the dynamically generated IDs. 警报确实不会显示动态生成的ID。

$('form').submit(function() {
    alert($(this).serialize());
    return false;
  });

The script is as follows: 脚本如下:

$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 0; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++;
            var printCounter = document.getElementById("product_counter");
            printCounter.value = x; 
            //increment
            $(wrapper).append('<div>' +
        '<div class="input-group">' +
        '<span class="input-group-addon">Nom</span>'+
                    '<input id="vch_invoice_item['+x+']” name="vch_invoice_item['+x+']” value="" type="text" class="form-control" placeholder="">'+
                 '</div>'+
                '<div class="input-group">'+
                   '<span class="input-group-addon">Description</span>'+
                  '<input id="vch_invoice_description['+x+']” name="vch_invoice_description['+x+']" value="" type="text" class="form-control" placeholder="">'+
                '</div>'+
            '<div class="input-group">'+
                   '<span class="input-group-addon">Prix à l’unité</span>'+
                  '<input onclick="bindPriceMask(this)" onfocus="bindPriceMask(this)" id="vch_invoice_unitcost['+x+']” name="vch_invoice_unitcost['+x+']” value="" type="text" class="unit form-control" placeholder="">'+
                '</div>'+
            '<div class="input-group">'+
                   '<span class="input-group-addon">Quantité</span>'+
                  '<input onclick="bindQtyMask(this)" onfocus="bindQtyMask(this)" id="vch_invoice_quantity['+x+']” name="vch_invoice_quantity['+x+']” value="" type="text" class="qty form-control" placeholder="">'+
                '</div>'+
            '<div class="input-group">'+
                   '<span class="input-group-addon">Taxe Fédérale</span>'+
                  '<input onclick="bindTaxMask(this)" onfocus="bindTaxMask(this)" id="vch_invoice_fedTax['+x+']” name="vch_invoice_fedTax['+x+']” value="" type="text" class="taxf form-control" placeholder="">'+
                '</div>'+
            '<div class="input-group">'+
                   '<span class="input-group-addon">Taxe Provinciale</span>'+
                  '<input onclick="bindTaxMask(this)" onfocus="bindTaxMask(this)" id="vch_invoice_provTax['+x+']” name="vch_invoice_provTax['+x+']” value="" type="text" class="taxp form-control" placeholder="">'+
                '</div>'+
        '<a href="#" class="remove_field">Supprimer</a><br><br><br>'+
                '</div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});

jsfiddle.net/52VtD/9763 Any help is greatly appreciated jsfiddle.net/52VtD/9763非常感谢您的帮助

1.Some of my closing quotes were made with my keyboard in french and hence were wrong. 1.我的一些结束语是用法语用法语键盘写的,因此是错误的。 2.I took out the [ ] in IDs. 2.我拿出ID中的[]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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