2014-10-20 49 views
1

我試圖想出一個解決方案,當用戶單擊表單上的某些字段時,不會讓用戶無意中撥打ajax如何禁用ajax引起的下拉列表的值更改?

例如,我有一個表單,包含City,State和ZipCode下拉列表。 當數據最初在屏幕上加載時,字段會填充數據。 然後,當城市或州或兩者都發生變化時,就會有一個ajax調用爲特定的城市/州組合填充ZipCode下拉菜單。

但是,會發生什麼事是用戶不小心點擊或標籤來指出下拉或城市的文本框和標籤,並調用ajax重新填充ZipCodes下拉列表並將其默認爲從數據庫檢索到的第一個。

如何禁用ajax調用時用戶選項卡進入或退出這些字段,如果沒有意圖更改ZipCode。

這是在用戶標籤輸入/輸出時被調用的函數。該功能被稱爲上onbeforedeactivate事件在兩個城市textbox和郵編dropdownlist

function StateSelected(city, state, ddlZipName) 
{ 
// city is a textbox 
// state is a ddl 

// var mySelect = state; 


    if ((state.selectedIndex > 0) && (city.value.length > 0)) 
    { 
    //instantiate XmlHttpRequest 

     // Checking if IE-specific document.all collection exists 
     // to see if we are running in IE 
     if (document.all) { 
      xhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
     } else { 
     // Mozilla - based browser 
      xhttp = new XMLHttpRequest(); 
     } 
     //hook the event handler 
     xhttp.onreadystatechange = HandlerOnReadyStateChange; 
     //prepare the call, http method=GET, false=asynchronous call 
     // alert(state.options[state.selectedIndex].value + 
     // " " + city.value + " " + ddlZipName); 
     xhttp.open("GET", "../sample.ashx?state=" + state.options[state.selectedIndex].value + "&city=" + city.value + "&ddlZipName=" + ddlZipName, false); 
     //finally send the call 
     xhttp.send();   
    } 
} 

onbeforedeactivate事件的代碼內部產生:

 txtCorporationLegalCity.Attributes.Add("onbeforedeactivate", "StateSelected(document.all['" + txtCorporationLegalCity.ClientID.ToString() + "'], document.all['" + ddlCorporationLegalState.ClientID.ToString() + "'], '" + ddlCorporationLegalZip.ClientID.ToString() + "')"); 
     ddlCorporationLegalState.Attributes.Add("onbeforedeactivate", "StateSelected(document.all['" + txtCorporationLegalCity.ClientID.ToString() + "'], document.all['" + ddlCorporationLegalState.ClientID.ToString() + "'], '" + ddlCorporationLegalZip.ClientID.ToString() + "')"); 

我只能認爲禁用ajax呼籲現在的。如果還有其他解決方案,請告訴我。

回答

0

我只記得StateSelected()調用的最後一個參數,所以如果再次調用相同的參數,就不要使用ajax並保留以前的值。

相關問題