简体   繁体   English

jQuery无法序列化通过Ajax加载的表单中的数据

[英]jquery can't serialize data from form loaded in via ajax

Has been a long night am struggling here. 在这里已经漫漫长夜挣扎。

I have a form which is loaded into a div via ajax. 我有一个通过ajax加载到div中的表单。

The button on the form makes another ajax call serialising the data, but when I call serialise it does not get the form data. 表单上的按钮会再次调用ajax来序列化数据,但是当我调用序列化时,它不会获取表单数据。

I cant understand why? 我不明白为什么?

THE FORM: 表格:

<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>

the ajax call attached to the submit button: 提交按钮上附加的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();
                }
            });
        }
    });

and here is the function I use to serialise the data: 这是我用来序列化数据的函数:

$.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;
    };

i did say it was a late night!!! 我确实说这是一个深夜!

stupid/tired me did not put in a name for the form added that and its working!!!! 愚蠢/累了,我没有为表单添加名称,它不能正常工作!

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

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