2017-04-26 97 views
0

我有2個HTML選擇使用jQuery多選級聯綁定使用AJAX調用它工作正常,問題是我想設置在主值,然後結合具體細節,然後設定值,但因爲它是阿賈克斯當試圖設置值的詳細值還不可用。執行腳本時,Ajax調用完成

表汽車 CarID INT CarModel(豐田) CarSubModel(雅力士)

function GetCarData() 
    { 
     var CarID = getQuerystringByName('C'); 
     if (CarID!=null) 
     { 
      $.ajax({ 
       type: "POST", 
       contentType: "application/json; charset=utf-8", 
       url: "Query.asmx/GetCarByID", 
       data: '{CarID: ' + CarID + '}', 
       dataType: "json", 
       success: function (Result) { 
        Result = Result.d; 
        txtCarName.value = Result.CarName; 
        $("#ddlCarModel").val(Result.ModelID); 
         BindSubModel();// Ajax to load Sub Models 
         $("#ddlCarSubModel").val(Result.SubModelID); 

我需要(一些如何)執行$( 「#ddlCarSubModel」)VAL(Result.SubModelID)。 亞ajax完成後,我不想使用等待方法(定時器)。

function BindSubModel() { 
     var ModelVal = $("#ddlCarModel").val(); 
     if (ModelVal != "") { 
       $.ajax({ 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      url: "Query.asmx/LoadCarSubModel", 
      data: '{ModelID: ' + ModelVal + '}', 
      dataType: "json", 
      success: function (Result) { 

Web方法

 public class CarSubModelInfo 
    { 
     public int SubModelID { get; set; } 
     public string SubModelName { get; set; } 
    } 

    [WebMethod] 
    [ScriptMethod] 
    public List<CarSubModelInfo> LoadCarSubModel(string ModelID) 
    { 
     string Conn = System.Configuration.ConfigurationManager.ConnectionStrings["GPS_TrackingConnectionString"].ConnectionString; 

     CarSubModelInfo driver = new CarSubModelInfo(); 
     List<CarSubModelInfo> SubModelInformation = new List<CarSubModelInfo>(); 
     DataSet ds; 
     using (SqlConnection con = new SqlConnection(Conn)) 
     { 
      using (SqlCommand cmd = new SqlCommand("select * from T_SubCarModels where ModelID=" + ModelID, con)) 
      { 
       con.Open(); 
       cmd.Connection = con; 
       cmd.CommandType = CommandType.Text; 
       using (SqlDataAdapter da = new SqlDataAdapter(cmd)) 
       { 

        ds = new DataSet(); 
        da.Fill(ds); 
       } 
      } 
     } 
     try 
     { 
      if (ds != null) 
      { 
       if (ds.Tables.Count > 0) 
       { 
        if (ds.Tables[0].Rows.Count > 0) 
        { 
         foreach (DataRow dr in ds.Tables[0].Rows) 
         { 
          SubModelInformation.Add(new CarSubModelInfo() 
          { 
           SubModelID = Convert.ToInt32(dr["id"]), 
           SubModelName = dr["name"].ToString() 
          }); 
         } 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
     return SubModelInformation; 
    } 
+0

使用'異步:FALSE'在阿賈克斯請找更多信息[這裏](HTTP:// API .jquery.com/jquery.ajax /) – Curiousdev

+0

@Curiousdev我認爲async默認是true? –

+0

@TobyMellor Thanxx ..我在寫作時很着急;) – Curiousdev

回答

0

$("#ddlCarSubModel").val(Result.SubModelID); 轉化爲成功的功能BindSubModel();

+0

結果是它不會在bindSubModel –

+0

提供的上一個AJAX的成功發送那裏,PARAM BindSubModel(Result.SubModelID); (SubModelID);功能(Result){(「#ddlCarSubModel」).val(SubModelID);' –

相關問題