jQuery 自动完成:根据选定的复选框更改 SQL 查询
假设我有一个文本框(带有自动完成功能 1.1,不是最新的 UI 版本)和一个复选框。 该文本框触发一个 asp.net .ashx 页面,该页面将在 sql 服务器上执行存储过程并返回结果。
一切正常,但我想添加另一个功能。 当选中该复选框时,我希望执行stored_procedure_1。 如果未选中该复选框,则必须执行stored_procedure_2。 默认情况下该复选框未选中。
我的问题: 我如何告诉 ashx 页面复选框是否被选中..? 默认情况下,自动完成将触发类似以下内容:autocompletedata.ashx?q=myname,并将执行stored_procedure_2,但是当选中该复选框时,它必须触发autocompletedata.ashx?q=myname&mycheckbox =begin
以便执行stored_procedure_1。
我是否必须添加一些 jQuery 代码才能通过选中的复选框? 我完全迷路了..
提前致谢。
表单元素:
<input id="search_employee" type="text" class="employee" />
<input type="checkbox" name="mycheckbox" value="begin" />
jQuery:
$().ready(function() {
$("#search_employee").autocomplete("includes/AutocompleteData.ashx", {
minChars: 3,
max: 15,
selectFirst: false,
scrollHeight: 300,
formatItem: function(data, i, n, value) {
if (value.split("_")[3]!== null)
{
return "<img style = 'width:40px;height:53px;float:left; margin:2px 5px 2px 0' src='/pictures/thumbs/"
+ value.split("_")[3] + "'/> " + value.split("_")[0] + "<br /><span class=smallname>" + value.split("_")[2] + "<br/>" + value.split("_")[4] + "</span>";
}
},
formatResult: function(data, value) {
return value.split("_")[0];
}
});
autocompletedata.ashx 的一部分:
Dim searchname As String = context.Request.QueryString("q")
Dim searchstart as string = context.Request.QueryString("mycheckbox")
Dim searchsql as string
if searchstart = "begin" then
searchsql = "stored_procedure_1"
else
searchsql = "stored_procedure_2"
end if
Dim conn As SqlConnection = New SqlConnection
conn.ConnectionString = ConfigurationManager.ConnectionStrings("MyConn").ConnectionString
Dim cmd As SqlCommand = New SqlCommand
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.CommandText = searchsql.ToString
cmd.Parameters.AddWithValue("@SearchText", searchname)
imagine i have one textbox (with autocomplete 1.1, not the latest UI version) and a checkbox.
The textbox triggers an asp.net .ashx page that will execute a stored procedure at the sql-server and return the results.
It's all working, but i want to add another feature.
When the checkbox is checked i want stored_procedure_1 to be executed.
If the checkbox is unchecked, stored_procedure_2 must be executed.
The checkbox is unchecked by default.
My question:
How do i tell the ashx page if the checkbox is checked or not..?
By default the autocomplete will trigger something like: autocompletedata.ashx?q=myname and that will execute stored_procedure_2, but when the checkbox is checked it must trigger autocompletedata.ashx?q=myname&mycheckbox=begin
so that stored_procedure_1 will be executed.
Do i have to add some jQuery code to pass the checked checkbox?
I am totally lost..
Thanks in advance.
Form elements:
<input id="search_employee" type="text" class="employee" />
<input type="checkbox" name="mycheckbox" value="begin" />
jQuery:
$().ready(function() {
$("#search_employee").autocomplete("includes/AutocompleteData.ashx", {
minChars: 3,
max: 15,
selectFirst: false,
scrollHeight: 300,
formatItem: function(data, i, n, value) {
if (value.split("_")[3]!== null)
{
return "<img style = 'width:40px;height:53px;float:left; margin:2px 5px 2px 0' src='/pictures/thumbs/"
+ value.split("_")[3] + "'/> " + value.split("_")[0] + "<br /><span class=smallname>" + value.split("_")[2] + "<br/>" + value.split("_")[4] + "</span>";
}
},
formatResult: function(data, value) {
return value.split("_")[0];
}
});
Part of autocompletedata.ashx :
Dim searchname As String = context.Request.QueryString("q")
Dim searchstart as string = context.Request.QueryString("mycheckbox")
Dim searchsql as string
if searchstart = "begin" then
searchsql = "stored_procedure_1"
else
searchsql = "stored_procedure_2"
end if
Dim conn As SqlConnection = New SqlConnection
conn.ConnectionString = ConfigurationManager.ConnectionStrings("MyConn").ConnectionString
Dim cmd As SqlCommand = New SqlCommand
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.CommandText = searchsql.ToString
cmd.Parameters.AddWithValue("@SearchText", searchname)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 .setOptions 设置其他参数:
这是编程方式,但在您的情况下:
这将执行以下 HTTP GET:
允许您通过
Request.QueryString< 访问它/code> ASHX 中的集合。
编辑
我的错 - 我正在考虑 AC 的另一个版本。
我认为该参数称为“extraParams”:
两者都尝试一下,其中一个将/应该起作用。 :)
呵呵。
You can use .setOptions to set additional parameters:
That is the programmatic way, but in your case:
That will perform the following HTTP GET:
Allowing you to access it via the
Request.QueryString
collection in the ASHX.EDIT
My bad - i was thinking another version of AC.
I think the param is called "extraParams":
Try them both, one will/should work. :)
HTH.