在jquery的$ .each()中维护ajax请求的顺序

时间:2013-10-26 05:23:38

标签: jquery ajax dynamic elements

嗨,根据我之前的问题得到解决here 我正面临另一个问题,即我使用jQuery的$ .each()绑定所有动态添加的元素,我想根据他们的订单验证每个元素,例如: - 第一次请求,第二次请求,然后是第三次请求,但是这里请求没有按照他们的顺序发送,例如: - 第二个元素,第一个元素,第四个元素,然后第三个,这继续布莱恩,我想知道我怎么能保留这些请求他们的订单。 这是我的整个jQuery代码: -

function update_db(){
           var udata = {};
           var adata = {};
           del_query = new Array();
           var confirm = 0;
           var vouchdt     = $("#dateinfo").val();         
           var a = "1";   
    if ($("#maintable tbody tr").length>0){

            var vendorid = $("#vendorinfo").val();  

        // prepare data to be updated
           $('[id^="dbtr_"]').each(function(index, table){

               var rid                       =  $('th:eq(0)', this).find('input:eq(0)').val();
               var basecatid                 =  $('td:eq(0)', this).find('select:eq(0)').val();
               var subvendorid               =  $('td:eq(0)', this).find('select:eq(0)').val();
               var prodid                    =  $('td:eq(1)', this).find('input:eq(1)').val();
               var productname               =  $('td:eq(1)', this).find('input:eq(0)').val();
               var quantity                  =  $('td:eq(2)', this).find('input:eq(0)').val();
               var muc                       =  $('td:eq(3)', this).find('#muid').val();
               var amt                       =  $('td:eq(4)', this).find('input:eq(0)').val();
               var reason                    =  $('td:eq(5)', this).find('textarea:eq(0)').val();
               var vat_percentage            =  $('td:eq(5)', this).find('input:eq(0)').val();
               var total_amount_before_vat   =  $('td:eq(5)', this).find('input:eq(1)').val();
               var vat_charged_in_bill       =  $('td:eq(5)', this).find('input:eq(2)').val();
               var invoice                   =  $('td:eq(6)', this).find('input:eq(0)').val(); 


               if(invoice =='' && prodid !=''){
                    alert("Invoice Number Cannot Be Empty");
                    $("#savetodb").prop("disabled", true);
                    a = "0";
                    return false;
                 }

        if(prodid !='' && invoice !=''){
        $.ajax({  
             type: "POST",  
             url: "../model/bulk_procurement_resold_at_lowprice.php", 
             data: {para :"upd", invno : invoice, product : prodid, date : vouchdt, quantity : qty},  
             success: function(result){
              if(result == "") {
                 alert(productname+" does not exist for invoice number "+id);
                 $("#savetodb").attr("disabled", "disabled");
              }
              if(result == "2") {
                 alert("Quantity "+ qty +" for "+productname+" can't be greater than the quantity procured for invoice number "+id);
                 $("#savetodb").attr("disabled", "disabled");
              }        
              if(result == "3") {
                 alert("Amount "+amt +" For "+productname+" does not exist for invoice number "+id);
                 $("#savetodb").attr("disabled", "disabled");
              }
           } 
        });

        }


            if (prodid != 'NA' && muc != '' && amt > 0 && rid != '')
            {
                if (quantity>0){
                    udata[rid] = {};
                    udata[rid]['sub_vendor_id'] = subvendorid;
                    udata[rid]['procurement_vendor_id'] = vendorid;
                    udata[rid]['product_id'] = prodid;
                    udata[rid]['quantity'] = quantity;
                    udata[rid]['measurement_unit'] = muc;
                    udata[rid]['amount_received'] = amt;
                    udata[rid]['reason'] = reason;
                    udata[rid]['vat_percentage'] = vat_percentage;
                    udata[rid]['total_amount_before_vat'] = total_amount_before_vat;
                    udata[rid]['vat_charged_in_bill'] = vat_charged_in_bill;
                    udata[rid]['invoice_number'] = invoice;
                }
                else{
                    del_query.push(rid);
                }
            }



            if(quantity !='' && invoice !=''){

            }           


        });
        // prepare data to be inserted
        var acount = 0;
        $('[id^="tr_"]').each(function(index, table){
                adata[acount]                = {}; 
                var basecatid                =  $('td:eq(0)', this).find('select:eq(0)').val();
                var subvendorid              =  $('td:eq(0)', this).find('select:eq(0)').val();
                var prodid                   =  $('td:eq(1)', this).find('input:eq(1)').val();
                var quantity                 =  $('td:eq(2)', this).find('input:eq(0)').val();
                var muc                      =  $('td:eq(3)', this).find('#muid').val();
                var amt                      =  $('td:eq(4)', this).find('input:eq(0)').val();
                var reason                   =  $('td:eq(5)', this).find('textarea:eq(0)').val();
                var vat_percentage           =  $('td:eq(5)', this).find('input:eq(0)').val();
                var total_amount_before_vat  =  $('td:eq(5)', this).find('input:eq(1)').val();
                var vat_charged_in_bill      =  $('td:eq(5)', this).find('input:eq(2)').val();
                var invoice                  =  $('td:eq(6)', this).find('input:eq(0)').val();



                if(invoice =='' && prodid !=''){
                   alert("Invoice Number Cannot Be Empty");
                   a = "0";
                   console.log(a);
                   return false;
                }

            if( quantity !='' && invoice !=''){

               var vouchdt     = $("#dateinfo").val();
                $.ajax({  
                     type: "POST",  
                     url: "../model/check_procurement_resold_with_invoice_number.php", 
                     data: { para : "upd", invno : invoice, product : prodid,  date : vouchdt, quantity : quantity},  
                     success: function(result){
                         alert(result);
                              if(result == "") {
                                alert(productname+" does not exist for invoice number "+invoice);
                                a = "0";
                                return false
                              }
                             if(result == "2") {
                                alert("Quantity "+ quantity +" for "+productname+" can't be greater than the quantity procured for invoice number "+invoice+" 1");
                                a = "0";
                                return false;
                             }
                     },
                        dataType: 'script',
                        async: false
                });             
            }

                if (prodid != 'NA' && muc != '' && amt != 0 )
                {
                    if (quantity>0){
                        adata[acount]['procurement_vendor_id'] = vendorid;
                        adata[acount]['product_id'] = prodid;
                        adata[acount]['quantity'] = quantity;
                        adata[acount]['measurement_unit'] = muc;
                        adata[acount]['amount_received'] = amt;
                        adata[acount]['reason'] = reason;
                        adata[acount]['date_of_resold'] = $("#dateinfo").val();
                        adata[acount]['vat_percentage'] = vat_percentage;
                        adata[acount]['total_amount_before_vat'] = total_amount_before_vat;
                        adata[acount]['vat_charged_in_bill'] = vat_charged_in_bill;
                        adata[acount]['sub_vendor_id'] = subvendorid;
                        adata[acount]['invoice_number'] = invoice;

                    }
                     acount++;
                 }

        });

     if(a =='1'){
        $.ajax({  
             type: "POST",  
             url: "../model/bulk_procurement_resold_at_lowprice.php", 
             data: {action:'updatedb',ud:udata,ad:adata,dd:del_query,username:'<?=$gotuser?>'},  
             success: function(result){
                 results = JSON.parse(result);
                 alert('Number of records Updated : '+results['utotal_s']+"\nNumber Of records Inserted : "+results['acnt']+"\nNumber of records Deleted  : "+results['dcnt']);
                 window.location.href="bulk_procurement_resold_at_lowprice.php?vendorinfo="+vendorid+"&dateinfo="+$("#dateinfo").val()+"&catinfo="+$("#catinfo").val();
             } 
        });
     }

    }
    return false;

}

1 个答案:

答案 0 :(得分:0)

Ajax请求是事件驱动的。因此,一旦请求收到其数据,就会调用success:函数。

您可以在接收数据时将接收到的数据添加到数组中,然后等待数组填满(使用if(holderArray.length = x){}或类似的东西)以及所有返回的数据以完成任务为了。

将订单号传递给变量中的成功部分,然后使用该数字输入,然后获取存储在数组中的数据。

holderArray[orderNumber] = result;