![](/img/trans.png)
[英]how to get google places autocomplete to work if the textbox is initially hidden and should be shown only after a postback
[英]How to postback without a button in an autocomplete textbox?
我将文本框绑定到DB,但是当我单击自动完成文本框中的名称并在文本框中选择该名称时,我不知道如何发送客户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));
}
}
使用选择事件: 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);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.