Ajax实现在textbox中输入内容,动态从数据库中模糊查询显示到下拉框中

网络整理 - 07-26

功能:在textbox中输入内容,动态从数据库模糊查询显示到下拉框中,以供选择

1.建立一aspx页面,html代码

<HTML>
    <HEAD>
        <title>WebForm1</title>
        <SCRIPT language="javascript">           
            //城市------------------------------
            function cityResult()
            {
                var city=document.getElementById("TextBox1");
                WebForm1.GetCityList(city.value,get_city_Result_CallBack);
            }
           
            function get_city_Result_CallBack(response)
            {
                if (response.value != null)
                {                   
                    //debugger;
                    document.getElementById("DropDownList1").style.display="block";
                    document.getElementById("DropDownList1").length=0;               
                var ds = response.value;
                    if(ds != null && typeof(ds) == "object" && ds.Tables != null)
                    {                   
                        for(var i=0; i<ds.Tables[0].Rows.length; i++)
                    {
                        var name=ds.Tables[0].Rows[i].city;
                      var id=ds.Tables[0].Rows[i].cityID;
                      document.getElementById("DropDownList1").options.add(new Option(name,id));
                    }
                    }
                }
                else
                {
                    document.getElementById("DropDownList1").style.display="none";
                }            
                return
            }
          
            function getData()
            {
                var province=document.getElementById("DropDownList1");