0

我有这样的 ajax 代码,它将表单数据传递到 createfinalinvoice.php。

serial_number 和 skid 是一些表单字段的一部分,它们是 name="serial_number[]" 和 name="skid[]"(见下文)

$(document).ready(function() {
    $("form#submit").submit(function() {
        // we want to store the values from the form input box, then send via ajax below
        var invoice_temp_id = $('#invoice_temp_id').attr('value');
        var customer = $('#customer').attr('value');
        var date = $('#date').attr('value');
        var shipdate = $('#shipdate').attr('value');
        var shipvia = $('#shipvia').attr('value');
        var ponumber = $('#ponumber').attr('value');
        var rep = $('#rep').attr('value');
        var invoicenotes = $('#invoicenotes').attr('value');
        var serial_number = $('#serial_number').attr('value');
        var skid = $('#skid').attr('value');
        var finalize_invoice = $('#finalize_invoice').attr('value');

        $.ajax({
            type: "POST",
            url: "includes/createfinalinvoice.php?",
            data: "invoice_temp_id="+ invoice_temp_id+
                "&customer="+ customer+
                "&date="+ date+
                "&shipdate="+ shipdate+
                "&shipvia="+ shipvia+
                "&ponumber="+ ponumber+
                "&rep="+ rep+
                "&invoicenotes="+ invoicenotes+
                "&serial_number="+ serial_number+
                "&skid="+ skid+
                "&finalize_invoice="+ finalize_invoice,
            success: function(data) {
                $('form#submit :input').not('input[type="submit"]').val("");
                $('div.success').fadeIn();
                $('div.success').html(data);
            }
        });

        return false;
    });
});

<form>
    <input type="text" name="serial_number[]" id="serial_number" class="serial_number">
    <input type="text" name="skid[]" id="skid" class="skid">
    <input type="text" name="serial_number[]" id="serial_number" class="serial_number">
    <input type="text" name="skid[]" id="skid" class="skid">
    <input type="text" name="serial_number[]" id="serial_number" class="serial_number">
    <input type="text" name="skid[]" id="skid" class="skid">
    <input type="text" name="serial_number[]" id="serial_number" class="serial_number">
    <input type="text" name="skid[]" id="skid" class="skid">
</form>

由于我传递了多个同名,因此将其放入数组中。我很难弄清楚如何让 AJAX 代码读取数组并传递它。到目前为止,代码只是将每个中的第一个作为字符串而不是数组传递。

4

1 回答 1

1

尝试使用序列化!创造奇迹

$( document ).on( 'submit', "form#submit", function( ) {
 $.ajax({
            type: "POST",
            url: "includes/createfinalinvoice.php?",
            data: $( this ).serialize(),
            success: function(data){
                $('form#submit :input').not('input[type="submit"]').val("");
                $('div.success').fadeIn();
                $('div.success').html(data);
                }
            });
        });
        return false;

});

我在这里也使用了 jquery .on,因为它比仅仅绑定到元素更强大,而且总体上更好。

于 2013-01-15T21:02:39.460 回答