English 中文(简体)
• 如何完成整整整整整座的 j
原标题:how to complete back end for auto complete jquery

I have a text box, Inside I want it to auto complete. The data for the auto complete is going to be given through the database.

这是我的格:

 var data = "autocompletetagdata.aspx"
    $("#item").autocomplete({
        source: data
    });


protected void Page_Load(object sender, EventArgs e) {
   return "[ word ,  hello ,  work ,  oi ,  hey ]";     
} 
问题回答

为此:

$("#item").autocomplete({
    source: function (request, response) {
        $.ajax({
            type: "POST",
            url: "autocompletetagdata.aspx/Search",
            data: { "search": request.term },
            contentType: "application/json; charset=utf-8",
            success: function (results) {
                var data = $.parseJSON(results);
                response($.map(data, function (item) { 
                    return { value: item } 
                }))
            }
        });
    }
});

在您的法典中,它成为一种网络方法:

[WebMethod]
public static void Search(string search)
{
    string[] list = new [] { "word", "hello", "work", "oi", "hey" };

    return list.Where(x => x.StartsWith(search));
}

I think what s wrong is you have to return json formatted data and not a comma separated string. Look at the demos page, it says:

The datasource is a server-side script which returns JSON data, specified via a simple URL for the source-option.

As per: http://www.srikanthtechnologies.com/blog/dotnet/jqueryautocomplete.aspx try returning a list of strings instead.


[WebMethod]
public static List<string> PopSearch()
{
 return new List<string> { "oi", "oi", "hey"};
}

j Query autocomplete期望json因此而工作。

return "[ word ,  hello ,  work ,  oi ,  hey ]";

For more complex data consider using the JavaScriptSerializer

由于似乎没有为你工作,确保你获得所覆盖的基本条件:

  • Are you loading both the jQuery and jQuery UI libraries? (autocomplete is jQuery UI)
  • Are you calling your jQuery code inside a DOM ready function?
  • Can you hit your web method directly in the browser and get data back?

Autocomplete with data is not really all that hard. First you need to establish your return type. The easiest tends to be in json format so:

主计长

// be sure to use echo, not return
echo json_encode($yourDataAsArrayOrObject);

这一部分回去了你的 j。

$("#item").autocomplete({
    // source should be a url to the controller function that returns the data
    source:  www.yoursite.com/controller/function ,
    search: function(e, ui) {}, // here you can manipulate other elements or items while the data is being retrieved,
    select: function(e, ui) { /* ui.item is item slected */ } // here you can manipluate things based on the item that was selected, for instance, save a list of sub data to another dom element
});

最后,

// You will need to use ._renderItem to items as they are received by database.
// For instance, if you want to display the dropdown items in a custom way.
$("#item")._renderItem = function(ul, item) {  
  // the item. depends on the information you got from server
  return ul.append($("<li></li>").prepend($("<h3></h3>").text(item.label)).append($("<p></p>").text(item.value)));
};

举例来说,所交数据用的是无法产生有效名词的单一报价([字、单、单、工作、 o、黑])。

简单地用两字取代单一字句,应当工作。 例:改为“序号”。 视你如何建设护卫服务器,你可能需要避免双重引用,如“[“字””、“”””等。

protected void Page_Load(object sender, EventArgs e) {
   return "[""word"", ""hello"", ""work"", ""oi"", ""hey""]";     
} 

Good luck.

@Troy Barlow,显然意味着

[WebMethod]
public static string[] Search(string search)
{
  string[] list = new[] { "word", "hello", "work", "oi", "hey" };
  return list.Where(x => x.StartsWith(search)).ToArray();
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签