【发布时间】:2015-12-18 17:13:07
【问题描述】:
尝试设置 jQuery 自动完成功能,但遇到了令人沮丧的问题。我的 aspx 页面中有这段代码:
<script type="text/javascript">
/*******************************/
/* Autocomplete the textboxes */
/* This responds fine if the javascript is inside the html tags and outside of the form tags, but
/* since this page has a Master Page File I can't do that.
/*******************************/
$(document).ready(function () {
$("#<%=txtPayers.ClientID %>").autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/Autocomplete.asmx/ISGetCompletionList") %>',
data: "{ 'prefixText': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('-')[0],
val: item.split('-')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
$("#<%=hfPayer.ClientID %>").val(i.item.val);
},
minLength: 1
});
});
</script>
在一个包含选项卡容器、选项卡面板、更新面板、div 等的相当大的文件中,我有这个文本框:
<asp:TextBox ID="txtPayers" runat="server" ClientIdMode="Static"></asp:TextBox>
我还有以下网络服务:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string[] ISGetCompletionList(string prefixText)
{
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["CLTDPL"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
List<string> Payers = new List<string>();
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT [PAYER_ID], [PAYER_TYPE] FROM [mos_Payer] WHERE " +
"PAYER_TYPE like '%' + @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefixText);
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
Payers.Add(string.Format("{0}-{1}", sdr["PAYER_TYPE"], sdr["PAYER_ID"]));
}
}
conn.Close();
}
return Payers.ToArray();
}
如果我把这一切都放在一个测试页面上并运行它,它就可以正常工作。但是,我需要它的实际页面有一个母版页文件,它在该上下文中不起作用。嗯,我第一次运行它,它工作正常。 But there are a few dropdowns on the page that, when something is selected, it hides and unhides other controls.一旦我这样做了,自动完成功能就会停止工作。
有什么想法吗?我也看过这个问题:
jQuery not working with Master Page
如果涉及更新面板,其中一个答案说使用.on()?我不知道如何实现它,我对 javascript 还是有点陌生。
【问题讨论】:
标签: javascript c# asp.net jquery-ui-autocomplete