jquery无法从通过ajax加载的表单序列化数据

时间:2011-05-10 11:07:08

标签: javascript jquery ajax serialization html-form

在这里度过了漫长的一夜。

我有一个通过ajax加载到div中的表单。

表单上的按钮进行另一个ajax调用序列化数据,但是当我调用serialise时,它不会获取表单数据。

我不明白为什么?

表格:

<form id="orderData">
        <table border="1" cellpadding="2" cellspacing="0" bordercolor="#d9d1cb" bgcolor="#666666" class="contact"  style="margin-top:10px;"s="s">
           <tr>
              <td colspan="2" bgcolor="#666666"><input type="text" name="name" id="name" style="width:250px;  border:0;" class="required contact"/></td>
              <td width="116" class="contact">Name:</td>
           </tr>
           <tr>
              <td colspan="2" bgcolor="#666666"><input type="text" name="surname" id="surname" style="width:250px;  border:0;" class="required contact"/></td>
              <td class="contact"><label for="name">Surname:</label></td>
           </tr>
           <tr>
              <td colspan="2" bgcolor="#666666"><input type="text" name="email" id="email" style="width:250px;  border:0;" class="required email contact"/></td>
              <td class="contact"><label for="email">Email:</label></td>
           </tr>
           <tr>
              <td colspan="2" bgcolor="#666666"><input type="text" name="telephone"  style="width:250px;  border:0;" id="telephone" class="contact"/></td>
              <td class="contact"><label for="telephone">Telephone:</label></td>
           </tr>
           <tr>
              <td colspan="2" bgcolor="#666666"><input name="mobile" type="text" id="mobile" style="width:250px;  border:0;" class="contact" /></td>
              <td class="contact">Mobile:</td>
           </tr>

           <tr>
              <td colspan="2" bgcolor="#666666"><textarea name="address" id="address" style="width:250px; height:50px; border:0; font-family:Arial, Helvetica, sans-serif;" cols="50" rows="2" class="required contact"></textarea></td>
              <td valign="top" class="contact"><label for="address">Address:</label></td>
           </tr>
           <tr>
              <td colspan="2" bgcolor="#666666"><input type="text" name="city" id="city" style="width:250px;  border:0;" size="32" class="required contact"/></td>
              <td class="contact"><label for="city">City:</label></td>
           </tr>
           <tr>
              <td colspan="2" bgcolor="#666666"><input type="text" name="postcode" style="width:250px;  border:0;" id="postcode" size="32" class="required contact"/></td>
              <td class="contact"><label for="postcode">Postcode:</label></td>
           </tr>
           <tr>
             <td colspan="2" align="right" valign="top" bgcolor="#666666"><select name="hearabout" id="hearabout" class="contact">
               <option value="Google">Google</option>
               <option value="Other search engine">Other Search Engine</option>
               <option value="ES Magazine">ES Magazine</option>
               <option value="Telegraph">Telegraph</option>
               <option value="Guardian Magazine">Guardian Magazine</option>
               <option value="Passesd by">Passed By</option>
               <option value="From a friend">From a friend</option>
               <option value="Other">Other</option>
             </select></td>
             <td valign="top" class="contact">How did you hear about us?</td>
           </tr>

           <tr>
              <td width="47" class="contact"><input name="send_email" type="hidden" value="send" /><input name="brochure" type="checkbox" id="brochure" value="1" /></td>
              <td width="197" class="contact"><div align="left"><strong>Request Brochure:</strong></div></td>
              <td class="contact"><input name="ordersamples" type="submit" class="button_contact" id="ordersamples" value="Order Samples" /></td>
           </tr>
        </table>
        </form>

附加到提交按钮的ajax调用:

$("body").delegate("#ordersamples", "click", function(e){
        e.preventDefault();
        var formData = $('#orderData').serializeObject();
            console.log(formData);
        if (swatchArray.length <=0){
            //alert('no samples');
            jQuery.facebox('<table width="100%" border="0" cellspacing="2" cellpadding="2"><tr><td>No samples selected.<br>To select a fabric sample simply click once on it and your selection will appear in the samples panel at the right of the page.<br>When you have made your selection (maximum 7 samples), fill out the form and click on ‘Order Samples’</td></tr><tr><td></td></tr></table>');
            //Prevent the submit event and remain on the screen

            return false;
        } else {
            //alert('has samples');
            var myUrl = 'sendswatch-data2.php';
            var formData = $('#orderData').serializeObject();
            console.log(formData);
            $.ajax({
                url: myUrl,
                data: {'swatchid[]':swatchArray, 'formdata':$('#orderData').serializeObject()},
                type: "POST",
                error: function(xhr, statusText, errorThrown){
                    // Work out what the error was and display the appropriate message
                },
                success: function(myData){
                    $('#tabsampleorder').html(myData);
                    $('.tabber').hide();
                    $('#tabsampleorder').show();
                }
            });
        }
    });

这是我用来序列化数据的函数:

$.fn.serializeObject = function(){
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };

1 个答案:

答案 0 :(得分:0)

我说这是一个深夜!!!

愚蠢/厌倦了我没有为表格添加一个名称及其工作!!!!