0

誰かがこのコードの何が問題なのか教えてくれる可能性はありますか?

ページに 4 つまたは 5 つのリンクがあり、これらは ajax_load 関数で読み込まれます。それらの間をクリックすると、これは常に機能します。リンクがクリックされると、常に Google Chrome に表示される ajax_load 関数に 2 つの console.log アラートがあります。

form_submit 関数を呼び出す [送信] ボタンをクリックすると、問題が発生します。常に機能します - コンソール ログに成功メッセージが表示されます - しかし、リンクに戻ると、ほとんどの場合、最初のコンソール ログ メッセージしか表示されません。(「form_submit」を処理する前に、2 つのメッセージが表示されました。)

到達しません:

  if (current_url_method != url + method) {   
    console.log('You got to the url + method part. But sometimes I dont get this far.'); 
    current_url_method = url + method; 

とにかく、誰かが何が悪いのか教えてくれたら、私は感謝します. ここにあります:

$(document).on("ready", function(){

//I want to load content into the '.page-content' class, with ajax

 console.log('load ajax when document starts. This always works.'); 
  var ajax_loaded = (function(response) {        

    $(".page-content")

     .html($(response).filter(".page-content"));       

    $(".page-content .ajax").on("click",ajax_load);  



   });

//use the ajax function below for submitting forms

var form_submit = (function(e) {
  console.log('form_submit is working. This always works.');         
  e.preventDefault();               

  var url = $(this).attr("action");       
  var method = $(this).attr("method");      


  var data = {}                 
  $(this).find("input, textarea, select").each(function(i){
    var name = $(this).attr("name");      
    var value = $(this).val();          

    data[name] =value;              

  }); 

  $.ajax({                    
    "url": url,                 
    "type": method,                
    "data": data,               
    "success": ajax_loaded,
    "error": function () {alert("bad");}    
  });

});

//the function below is called by links that are described 
//with the class 'ajax', or are in the div 'menu' 

var history = [];                 

var current_url_method;               

var ajax_load = (function(e) {  


console.log('load ajax on clicks. This always works.');         
  e.preventDefault();               


  history.push(this);               
  var url =$(this).attr("href");          
  var method = $(this).attr("data-method");   

  if (current_url_method != url + method) {   
    console.log('You got to the url + method part. But sometimes I dont get this far.'); 
    current_url_method = url + method;      

$.ajax({                  
  url: url,               
  type: method,  
  async: false,                       
  success: ajax_loaded       
});
   }

});


//monitor for clicks from menu div, or with
//the ajax class, or the 'submit button'.
//why the trigger?

$("#menu a").on("click",ajax_load);
$(".ajax").on("click",ajax_load);
$("#menu a.main").trigger("click");
$(".search-box form").on("submit", form_submit);


});
4

2 に答える 2