javascript - POST 表单动态生成的输入

标签 javascript php jquery forms submit

我有一个表单,单击调用附加函数的按钮即可动态添加输入。提交表单后,我只能访问在页面加载时创建的输入。我使用以下调试代码仔细检查了这一事实。该警报确实不显示动态生成的 ID。

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

脚本如下:

$(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 非常感谢任何帮助

最佳答案

1.我的一些结束语是用法语键盘输入的,因此是错误的。 2.我把ID中的[]去掉了。

关于javascript - POST 表单动态生成的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27673479/

相关文章:

javascript - 如何在不刷新整个 html 页面的情况下刷新动态表

javascript - 如何在重新加载页面后保存按钮的值?

JavaScript:分配中的左侧无效?

Phpunit,模拟 SoapClient 是有问题的(模拟魔术方法)

php - 我可以声明一个 php 函数抛出异常吗?

javascript - 如果打开新选项卡,其他选项卡中的 Jquery NiceScroll 仍保持事件状态

jquery - 彼此相邻的两个 Bootstrap 轮播不会对齐

javascript - 如何在 typescript 的输入搜索框中添加去抖动时间?

Javascript 图像预览始终处于横向模式错误

php - 使用 PHP ftp_get() 检索具有通配 rune 件名的文件