所以我已经有一段时间(超过一周)搜索谷歌和stackoverflow以及所有这些。似乎无法获得信息汇编来完成这项工作。
[ASPX 页面]
<span>Make</span> <asp:TextBox ID="Make_SearchBox" runat="server" ToolTip="Enter Make"></asp:TextBox>
<span>Model</span> <asp:TextBox ID="Model_SearchBox" runat="server" ToolTip="Enter Model"></asp:TextBox>
[JavaScript]
<%--Autocomplete Function--%>
<script type="text/javascript">
var value = "";
$(document).ready(function () {
$("#PageContent_Make_SearchBox").autocomplete("/WebHandlers/AutoComplete.ashx",{
extraParams: { field: "Make" },
autoFill: false
});
$("#PageContent_Model_SearchBox").autocomplete("/WebHandlers/AutoComplete.ashx", {
extraParams: { field: "Model" },
autoFill: false
});
});
</script>
[ASHX WEBHANDLER - C#]
public void ProcessRequest(HttpContext context)
{
// INITIALIZING NEEDED VARS
string query = "";
string sql = "";
// CHECK "field" QUERY AND PASS TO SWITCH
string field = context.Request.QueryString["field"];
if (field != null)
{
switch (field)
{
case "Make":
query = context.Request.QueryString["q"];
sql = "Select DISTINCT Make from Vehicle Where Make LIKE '%" + query + "%'";
break;
case "Model":
query = context.Request.QueryString["q"];
sql = "Select DISTINCT Model from Vehicle Where Model LIKE '%" + query + "%' AND Make LIKE (((MAKE TEXTBOX VALUE)));
break;
}
}
string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
// CONNECT TO DATABASE, RUN QUERY AND RETURN DATA
using (SqlConnection connection = new SqlConnection(connStr))
using (SqlCommand command = new SqlCommand(sql, connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
context.Response.Write(reader.GetString(0) + Environment.NewLine);
}
}
}
}
以上内容与make文本字段完全配合。当我在make字段中输入“toy”时,它会像这样传递:
GET http://localhost:26724/WebHandlers/AutoComplete.ashx?q=toy&field=Make
我想要做的是在用户获得品牌并开始在模型字段中写入之后,它应该只搜索与该品牌相关的模型。我想 C# 方面很简单,只是 SQL 查询,但是如何让 JQuery 获取make文本字段的值并将其添加到查询字符串中。请注意,它只需要在make文本框已满而不是在 $document.ready 时才获取它的值。
谢谢大家。我一定会选择一个最佳答案。