![](/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.