如何在自动完成文本框中没有按钮的情况下回发?

时间:2015-09-04 18:31:39

标签: javascript jquery asp.net autocomplete postback

我将文本框绑定到数据库,但是当我点击自动填充文本框中的名称并在文本框中选择名称时,我不知道如何发送客户的ID以在其他页面中显示其数据。

<script type="text/javascript">
    $(document).ready(function () {
        $('#txtSearch').autocomplete({
            source: 'SearchHandler.ashx'
        });
    });

</script>

这是背后的代码

public class SearchHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context) 
    {
        string term = context.Request["term"] ?? "";
        List<string> listSearch = new List<string>();
        List<string> IDsearch = new List<string>();

        string cs = ConfigurationManager.AppSettings["dbpath"];
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("searchFriend", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter()
            {
                ParameterName="@term",
                Value = term
            });
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();

            while(dr.Read())
            {
                listSearch.Add(dr["UInfo"].ToString());
                IDsearch.Add(dr["UId"].ToString());
            }
        }

        JavaScriptSerializer js = new JavaScriptSerializer();
        context.Response.Write(js.Serialize(listSearch));  
    }
}

1 个答案:

答案 0 :(得分:0)

使用select事件: http://api.jqueryui.com/autocomplete/#event-select

       $('#txtSearch').autocomplete({
            source: 'SearchHandler.ashx',
            select: function( event, ui ) {
               //submit form here
               document.forms[0].submit();
           }
        });

如果您需要检索所选值,请使用close-event,然后从文本框中检索值并按您的需要使用它:

       $('#txtSearch').autocomplete({
            source: 'SearchHandler.ashx',
            close: function( event, ui ) {

                 var userid = $(this).val();

                 //open new window with customer info
                 window.open('CustomerInfo.aspx?Customer=' + userid);


           }
        });