2017-04-26 105 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);' –

相关问题