2011-11-01 111 views
1

我有一個遞歸函數generateHtml這是在表單提交函數中調用。我的問題是success部分。即使我刪除了成功部分的內容,只是把警報,它不起作用。它也沒有出現任何錯誤。成功的部分不工作.ajax()

function generateHtml(count,total,pars,files) { 

    if (count == 1) { 
     f = 'f='; 
     allfiles = files; 
     allpars = pars; 
    } 

    fn = randomToN(99999999, 0); 
    f = f + fn + '|'; 
    dat = allpars + "&file=" + allfiles[count] + "&fn=" + fn + '&i=' + count; 
    alert(dat); 

    $.ajax({ 
     url: "../../ii/getindreports.php", 
     type: 'POST', 
     data: dat, 
     async:true, 
     dataType: 'html', 
     error: function() { 
      alert('ERROR !!!!'); 
     }, 
     complete: function() { 
      alert('test'); 
      // -------------------------Success part---------------// 
      $('#merobar').html($('#merobar').html() + '<br>Report '+ count +' Compiled.'); 

      // if count not equal to total we still have html to process so call the function again 
      if (count != total) { 
       count = count + 1; 
       alert(count); 
       generateHtml(count, total, 0, 0); 
      } else { 
       alert('make pdf now '); 
       fnpdf = randomToN(99999999, 0); 
       f = f + '&pdf=' + fnpdf; 

       $.ajax({ 
        url: "../../ii/makebulkhtmlpdf.php", 
        type: 'POST', 
        data: f, 
        async:true, 
        dataType: 'html', 
        success: function() { 
         //alert('<br>Report Generation Completed'); 
         $('#merobar').html($('#merobar').html() + '<br>Report Generation Completed'); 
         $('#merobar').html($('#merobar').html() + '<br><a href=\"../../temp/temp_'+fnpdf+'.pdf\" target=\"_blank\">Click To Download</a>'); 
        } 
       }); 
      }//end of else 
        // -------------------------Success part--------------// 
     }//end of success 
    });// end of first .ajax function 
    return true; 
}//end of function 

$('#sliderform').submit(function() { 
    //alert("here"); 
    var files = new Array(); 
    var checked = $("input:checkbox[name=files]:checked").length > 0; 

    if (!checked) { 
     alert("Please select at least one Report"); 
     return false; 
    } 

    i = 1; 
    $("input:checkbox[name=files]:checked").each(function() { 
     //alert($(this).val()); 
     if ($(this).val() != '') { 
      files[i] = $(this).val(); 
      i = i + 1; 
     } 
    });//end of .each 

    //alert(files); 
    $('#merobar').html('File Processing ...'); 
    pars = $(this).serialize(); 
    total = $("input:checkbox[name=files]:checked").length; 

    generateHtml(1,total,pars,files); 
});//end of submit function 
+0

你介意用正確的縮進格式化你的代碼部分,所以它變得更容易閱讀嗎? –

+2

只是一些樣式指針:使用'var'關鍵字來聲明變量;爲了便於閱讀,請始終保持您的代碼縮進。另外,對於你的問題:'complete()'不是「成功」回調,'success()'是。 –

+0

你好我以前成功了,所以我試着成功。但它似乎在IE中工作。在鉻中工作 – abnab

回答

0

我不知道這是什麼會做,但我重寫所有的代碼更接近我會做事情的方式,將良好的作風和做法,適當聲明並不意味着是全局變量。

我的代碼除了缺少var關鍵字之外沒有看到任何語法錯誤,因此如果某些內容不起作用,則可能有其他代碼存在語法問題,導致您的帖子中的代碼無法運行。

function generateHtml(count, total, pars, files) { 

    var f = ''; 
    var allfiles = []; 
    var allpars = []; 
    var fn = 0; 

    if (count == 1) { 
     f = 'f='; 
     allfiles = files; 
     allpars = pars; 
    } 

    fn = randomToN(99999999, 0); 
    f = f + fn + '|'; 
    dat = allpars + "&file=" + allfiles[count] + "&fn=" + fn + '&i=' + count; 
    alert(dat); 

    $.ajax({ 
     url: "../../ii/getindreports.php", 
     type: 'POST', 
     data: dat, 
     async: true, 
     dataType: 'html', 
     error: function() { 
      alert('ERROR !!!!'); 
     }, 
     complete: function() { 
      alert('test'); 
      // -------------------------Success part---------------// 
      $('#merobar').append('<br>Report '+ count +' Compiled.'); 

      // if count not equal to total we still have html to process so call the function again 
      if (count != total) { 
       count = count + 1; 
       alert(count); 
       generateHtml(count, total, 0, 0); 
      } else { 
       alert('make pdf now '); 
       var fnpdf = randomToN(99999999, 0); 
       f = f + '&pdf=' + fnpdf; 

       $.ajax({ 
        url: "../../ii/makebulkhtmlpdf.php", 
        type: 'POST', 
        data: f, 
        async:true, 
        dataType: 'html', 
        success: function() { 
         //alert('<br>Report Generation Completed'); 
         $('#merobar').append('<br>Report Generation Completed'); 
         $('#merobar').append('<br><a href=\"../../temp/temp_' + fnpdf + '.pdf\" target=\"_blank\">Click To Download</a>'); 
        } 
       }); 
      }//end of else 
        // -------------------------Success part--------------// 
     }//end of success 
    });// end of first .ajax function 
    return true; 
}//end of function 

$('#sliderform').submit(function() { 
    //alert("here"); 
    var files = []; 
    var checked = $("input[name=files]:checked").length > 0; 

    if (!checked) { 
     alert("Please select at least one Report"); 
     return false; 
    } 

    var i = 1; 
    $("input[name=files]:checked").each(function() { 
     //alert($(this).val()); 
     if ($(this).val() != '') { 
      files.push($(this).val()); 
     } 
    });//end of .each 

    //alert(files); 
    $('#merobar').html('File Processing ...'); 
    var pars = $(this).serialize(); 
    var total = $("input[name=files]:checked").length; 

    generateHtml(1, total, pars, files); 
});//end of submit function