2011-06-27 50 views
0

我已經在需要暫停確認框的情況下被觸發。這是我做了什麼:需要暫停javascript的確認窗口

function onBeforeClientInsert(record) { 
    var eventtype = parseInt(record. < %= CEO.FieldEvaluator.GetEvaluatorByDId("EVENT_TYPE_ID").GetFieldDataFieldId() % >); 
    var begindate = record. < %= CEO.FieldEvaluator.GetEvaluatorByDId("BeginDate").GetFieldDataFieldId() % > ; 
    var enddate = record. < %= CEO.FieldEvaluator.GetEvaluatorByDId("EndDate").GetFieldDataFieldId() % > ; 

    $.ajax({ 
     type: "POST", 
     url: "Data.aspx/CheckInsertRecord", 
     data: "{EventType:'" + eventtype + "',BeginDate:'" + begindate + "'," + "EndDate:'" + enddate + "' }", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function(msg) { 

      if (msg.d == "No duplicate") { 

      } else { 
       alert(msg.d); 
       eval("var data = " + msg.d + ";"); 

      } 
      var i = 0; 
      alert(i); 
      alert(data[i]); 
      do { 
       $("#beginDate").html(data[i].BeginDate); 
       $("#eventTypeID").html(data[i].EVENT_TYPE_ID); 
       $("#endDate").html(data[i].EndDate); 
       $("#beginlatlong").html(data[i].BeginLATLONG); 
       $("#endlatlong").html(data[i].EndLATLONG); 

       var modal = document.getElementById('Div1'); 
       modal.style.display = ''; 
       modal.style.position = 'fixed'; 
       modal.style.zIndex = '100'; 
       modal.style.left = '30%'; 
       modal.style.top = '10%'; 


       var screen = document.getElementById('modalScreen'); 
       screen.style.display = ''; 
       i++; 
       if (confirm("Are you sure you want to continue?") == false) { 
        hide(); 
        continue; 
       } 


      } 

      while (msg.d != null); 
     } 
    }); 


    if (confirm("Are you sure you want to insert this new record ?") == false) { 
     hide(); 
     return false; 
    } 
    if (Page_ClientValidate("<%= CEO.GridUtils.Global_ValidationGroupName%>")) { 
     hide(); 
     SetPostBackCause('INSERT'); 
     return true; 
    } 
    return false; 
} 

所以,這個問題一直是

if (confirm("Are you sure you want to insert this new record ?") == false) { 
    hide(); 
    return false; 
} 

將確認框中

if(confirm("Are you sure you want to continue?")==false){ 
    hide(); 
    continue; 
} 

後立即運行,但我想它被叫停直到用戶點擊第一個確認框上的某個東西。你可以讓我知道如何做到這一點?如果我以錯誤的方式接近它,你還可以讓我知道任何其他方式來做到這一點嗎?

+0

一旦你得到這個代碼的工作,**請**發佈在[代碼評論](http://codereview.stackexchange.com)的清理建議。 –

+0

@Matt:當然我會發布它 – Sayamima

回答

2

Ajax是異步的。

您需要將全部相關代碼移入success回調。

success: function (msg) { 
    // snip ... 

    if (confirm("Are you sure you want to insert this new record ?") == false) { 
     hide(); 
    } 
    if (Page_ClientValidate("<%= CEO.GridUtils.Global_ValidationGroupName%>")) { 
     hide(); 
     SetPostBackCause('INSERT'); 
    } 
} 

可能使用async: false發出請求採用同步,但我推薦這個。如果你想讓它的第一個對話框後運行

if (confirm("Are you sure you want to insert this new record ?") == false) { 
     hide(); 
     return false; 
    } 
    if (Page_ClientValidate("<%= CEO.GridUtils.Global_ValidationGroupName%>")) { 
     hide(); 
     SetPostBackCause('INSERT'); 
     return true; 
    } 

您的AJAX回調中:

+0

我試過這個......但第二個確認框沒有被調用 – Sayamima

1

您需要將所有這一切。目前,您設置回調,然後調用上面的代碼。如果您的互聯網連接速度很慢,您看到的第一個對話框理論上會在您看到的第二個對話框後出現。

+0

我試圖保持它在成功功能,但第二個符合框不會顯示出來。 – Sayamima

0

你有沒有考慮到$ .ajax的async:false選項?

+0

否我會嘗試的 – Sayamima