2013-09-30 54 views
0

每次我失去對ID字段的注意力時,將調用alert("Start AJAX: " + EmplID);,但AJAX調用會進行兩次:第二次ID字段失去焦點並且完全不是第一次。所有建議都會有所幫助。jQuery ajax調用不會觸發第一次調用

$("#ID").focusout(function() { 
    var EmplID = $(this).val(); 
    alert("Start AJAX: " + EmplID); 
    $.ajax({ 
     type: "POST", 
     url: "program_includes/Employee_help.php", 
     cache: false, 
     data: { 
      value: 4, 
      id: EmplID 
     } 
    }).done(function(msg) { 
     alert(msg); 
     parseScript(msg); 
     $.ajax({ 
      type: "POST", 
      cache: false, 
      url: "program_includes/Customer_help.php", 
      data: { 
       value: 4, 
       id: $("#Kundennummer").val(), 
       Employee: "Yes" 
      } 
     }).done(function(msg) { 
      alert(msg); 
      parseScript(msg); 
      $("#Name").val($("#customers_Name").val()); 
     }).fail(function(msg) { 
      alert("Fail2 : " + msg); 
     }); 
    }).fail(function(msg) { 
     alert("Fail1 : " + msg); 
    }); 
}); 
+8

我的建議是**不**使用'alert'進行調試。這不是1997年。使用調試器進行調試。有一個內置於您的瀏覽器。這總是我的建議,但在這裏特別相關,因爲'alert'會干擾焦點邏輯...... –

+0

'console.log'會比'alert'更好 –

+0

似乎已經解決了這個問題。謝謝。 – user2831126

回答

0

使用event.stopPropagation()繼從T.J. Crowder

我的建議是不要用警報進行調試。這不是1997年。使用調試器進行調試。有一個內置於您的瀏覽器。這一直是我的建議,但特別是與此有關,如警報,重點邏輯干擾..

這是一個非常明確的一點,除非IMO你在做移動開發不使用警報。你應該使用控制檯。所以,簡單地更換

alert(); //badness 

console.info(); //goodness, look at the console 

然後按F12在瀏覽器中,你會看到在您的控制檯中的日誌。現在甚至最新的IE都有JS控制檯。

+0

我不確定在最新版本的IE中它是否仍然是這種情況,但對於早期版本,必須在代碼執行之前按F12才能使'console'對象存在。 –

+0

好點@AnthonyGrist –

+0

控制檯能正常工作,無需在IE中打開控制檯,至少在IE10和IE8中。所以自2008年以來一直在努力。 – user2831126

0

而不是專注,請嘗試使用模糊。 jQuery的文檔說如下:

The focusout event is sent to an element when it, or any element inside of it, loses 
focus. This is distinct from the blur event in that it supports detecting the loss of  
focus on descendant elements (in other words, it supports event bubbling). 

或者在事件的內容處理程序

+0

這並沒有改變任何東西,但刪除警報解決了問題。 感謝您的回答。 – user2831126