2011-10-21 92 views
0

你可以請幫助我如何獲得自動完成下拉選定的項目的價值?我能夠填充數據,我想單獨獲取所選項目的值。以下是我正在使用的片段。自動完成與ASP.Net的JQuery

$(document).ready(function() { 
$("#txtTest").autocomplete({ 
    source: function (request, response) { 
     $.ajax({ 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      url: "Webservice.asmx/GetNames", 
      data: "{'prefix':'" + request.term + "'}", 
      dataType: "json", 
      async: true, 
      success: function (data){ 
       response($.map(data, function(item) 
       { return item ; })); 
      }, 
      error: function (result) { 
       alert("Due to unexpected errors we were unable to load data"); 
      } 
     }); 
    }, 
    minLength:2 
}); 
}); 

感謝

回答

0

下面是這種情況的一個例子。

Default.aspx的

<script type="text/javascript"> 
$(document).ready(function() { 
    $("input#<%=txtKelime.ClientID %>").autocomplete('Ara.aspx').result(function(event, item) { 
     $("#<%=txtGizliAlan.ClientID %>").val(item.toString().split(",")[1]); 
    }); 
}); 
</script> 

<form runat="server" id="form1"> 
    <asp:textbox id="txtKelime" runat="server" /> 
    <asp:textbox id="txtGizliAlan" runat="server" style="display:none" /> 
</form> 

Search.aspx

protected void page_load(object sender, EventArgs e) { 
string strKelime = Request.QueryString["q"]; 
DataTable dt = Database.GetDataTable("Select * from TableName where SearchString like '%" + SearchWord + "%'"); 
foreach (DataRow dr in dt.Rows) 
{ 
Response.Write(dr["alanAdi"].ToString() + "|" + dr["id"].ToString() + Environment.NewLine); 
} 
} 

你可以看到這個演示。 http://jquery.bassistance.de/autocomplete/demo/

+0

選定的值必須作爲客戶端的一部分而不使用.aspx – pal

0

試試這個

$(document).ready(function() { 



$("#txtTest") 
      // don't navigate away from the field on tab when selecting an item 
      .live("keydown", function (event) { 
       if (event.keyCode === $.ui.keyCode.TAB && 
         $(this).data("autocomplete").menu.active) { 
        event.preventDefault(); 
       } 
      }) 
.autocomplete({ 
    source: function (request, response) { 
     $.ajax({ 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      url: "Webservice.asmx/GetNames", 
      data: "{'prefix':'" + request.term + "'}", 
      dataType: "json", 
      async: true, 
      success: function (data){ 
       response($.map(data, function(item) 
       { return item ; })); 
      }, 
      error: function (result) { 
       alert("Due to unexpected errors we were unable to load data"); 
      } 
     }); 
    }, 
    minLength:2 
}); 
}); 

您可以按Tab鍵或選擇項目選擇項目。