简体   繁体   English

为什么表单序列化在这种情况下不起作用?

[英]Why form serialize does not work in this case?

I have a form which I wanna send via AJAX so I'll use serialize jQuery function. 我有一个表单,我想通过AJAX发送,所以我将使用序列化jQuery函数。

This is the code (not all the elements has "name", but anyway the alert should display something): 这是代码(并非所有元素都有“名称”,但无论如何警报应该显示一些内容):

http://jsfiddle.net/T6Rz3/2/ http://jsfiddle.net/T6Rz3/2/


JS JS

$('#add-producto').click(function (e) {

    alert($('#datos-add').serialize())

    $.ajax({
        type: "POST",
        url: "insertar.php",
        data: $('#datos-add').serialize(),
        success: function (data) {
            alert(data);
        }
    });


    e.preventDefault();

});

HTML HTML

<form id="#datos-add">
    <div class="form-horizontal">
        <div class="modal modal-block">
            <div class="modal-header">
                    <h3>Añadir productos</h3>

            </div>
            <div class="modal-body">
                <div class="control-group">
                    <label class="control-label">Marca</label>
                    <div class="controls">
                        <input type="text" name="marca" id="marca" class="input-xlarge">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Producto</label>
                    <div class="controls">
                        <input type="text" name="descr" id="descr" class="input-xlarge">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Tallajes</label>
                    <div class="controls controls-row">
                        <input id="tallajes" value="España">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Referencias</label>
                    <div class="controls controls-row">
                        <input id="referencias" class="input-xlarge">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Número de tallas</label>
                    <div class="controls controls-row">
                        <input type="number" min="1" max="99" value="5" id="numero-tallas" class="span1">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Primera talla</label>
                    <div class="controls controls-row">
                        <input type="text" id="primera-talla" value="XS" class="span1">
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <div class="btn-group">
                    <button id="borrar" class="btn" type="button">Borrar todo</button>
                    <button id="generar" class="btn btn-primary" type="button">Generar tabla</button>
                </div>
            </div>
        </div>
        <div class="form-horizontal">
            <div id="formulario-medidas" class="modal modal-block">
                <div class="modal-header">
                        <h4>Tabla de medidas</h4>

                </div>
                <div class="modal-body">
                    <table id="tabla-medidas" class="table table-bordered"></table>
                </div>
                <div class="modal-footer">
                    <button id="medidas-completadas" class="btn btn-success" type="button">Medidas completadas</button>
                </div>
            </div>
            <div id="formulario-tallajes" class="modal modal-block">
                <div class="modal-header">
                        <h4>Tabla de tallajes</h4>

                </div>
                <div class="modal-body">
                    <table id="tabla-tallajes" class="table table-bordered"></table>
                </div>
                <div class="modal-footer">
                    <button id="add-producto" class="btn btn-block btn-large btn-danger" type="button">Añadir producto</button>
                </div>
            </div>
        </div>
    </div>
</form>

Do you see what I'm doing wrong? 你看到我做错了吗?

Any tip, advide or help will be appreciated, and if you need more info, let me know and I'll edit the post. 任何提示,建议或帮助将不胜感激,如果您需要更多信息,请告诉我,我将编辑该帖子。

Your form id is "datos-añadir" but your JavaScript refers to "datos-add". 您的表单ID是“datos-añadir”,但您的JavaScript指的是“datos-add”。

Here is a jsfiddle fork that works. 这是一个有效的jsfiddle分支 All I did was fix the reference: 我所做的就是修复参考:

$('#add-producto').click(function (e) {

    var $form = $('#datos-añadir');
    alert($form.serialize());
    return false;

Fist of all, there is a typo. 所有的拳头,有一个错字。 It should be <form id="datos-add"> instead of <form id="#datos-add"> . 它应该是<form id="datos-add">而不是<form id="#datos-add">

But I also think you are serializing it the wrong way. 但我也认为你是以错误的方式序列化它。

When you do a POST ajax call, you shoud use .serializeArray() , instead of .serialize() . 当你进行POST ajax调用时,你应该使用.serializeArray()而不是.serialize()

The .serialize() will join all key/values like a query string. .serialize()将连接所有键/值,如查询字符串。 The .serializeArray() will include all key/values in the request. .serializeArray()将包含请求中的所有键/值。

In short, try: 简而言之,尝试:

$('#add-producto').click(function (e) {
    $.ajax({
        type: "POST",
        url: "insertar.php",
        data: $('#datos-add').serializeArray(),
        success: function (data) {
            alert(data);
        }
    });
    e.preventDefault();
});

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

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