2010-10-26 50 views
0

想象一下,我有一個文本框(自動完成1.1,而不是最新的UI版本)和一個複選框。 該文本框觸發一個asp.net .ashx頁面,該頁面將在sql-server上執行一個存儲過程並返回結果。jQuery自動完成:根據選定的複選框更改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)   

回答

1

您可以使用.setOptions設置附加參數:

ac.setOptions({ params: { paramOne:'somevalue', somethingelse:'foobar' } }); 

這是編程的方法,但在你的情況:

$("#search_employee").autocomplete("includes/AutocompleteData.ashx", { 
    params: { flag:'true' }, // just grab the checked attr beforehand 
    ...//rest of your AC stuff 

將執行以下HTTP GET:

GET: includes/AutocompleteData.ashx?flag=true 

允許您通過ASHX中的Request.QueryString集合訪問它。

編輯

我的壞 - 我在想交流的另一個版本。

我認爲帕拉姆被稱爲 「extraParams」:

$("#search_employee").autocomplete("includes/AutocompleteData.ashx", { 
    extraParams: { flag:'true' } 

都去嘗試一下,一會/應該工作。 :)

HTH。

+0

感謝您幫助我在正確的方向:) 當我添加「extraParams:{'mycheckbox':'開始'}」它確實會調用stored_procedure_1。這是更進一步的。 但它應該只在選中複選框時添加這些額外參數(否則返回false)。 我如何(以及在​​哪裏)動態設置複選框的值? – 2010-10-26 13:04:48

+0

你是什麼意思「返回false」?通過你的代碼的外觀,如果CB被選中,你想調用SP1,否則SP2 - 是不是正確?你不能傳遞「begin」或null,保持簡單 - 只需將checked屬性直接傳遞給ASHX:'extraParams:{flag:$(「#idOfYourCheckbox」)。attr(「checked」)} 。 – RPM1984 2010-10-26 20:59:23

+0

謝謝,這樣做! – 2010-10-27 08:38:23