我有2个HTML选择使用jQuery多选择级联绑定使用ajax调用它工作正常问题是我想在master中设置值然后绑定细节然后设置值详细但是因为它是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) {
网络方法
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 :(得分:0)
放$("#ddlCarSubModel").val(Result.SubModelID);
在函数BindSubModel();