<input id="comboBox" data-options='{"url":"Home/Load","lengthText": 4,"timeWait":300 }' type="text" /> <input id="comboBox1" data-url="Home/Load" type="text" />
$().ready(function () { $("#comboBox") .query() .bind("query_data", function (e, data) { // – <div id=”panel”/> }) .bind("query_error", function (e, data) { // }) .bind("query_reset", function () { // <div id=”panel”/> }) .bind('query_start query_end', function (e) { // – $(this).css({ backgroundColor: e.type == "query_start" ? "#fdf5e6" : "#FFF" }); }); });
(function ($) { function query(target) { this.$target = $(target); // this.options = this.$target.data('options') || { url: this.$target.data("url") }; if (!this.options.url) // url - throw Error(" ..."); // this.options = $.extend({ lengthText: 3, timeWait: 300 }, this.options); }; query.prototype = { handler: function (val) { // if(this.def ) this.def.abort() ; if (val.length < this.options.lengthText) { // this.$target.trigger('query_reset'); return; }; var self = this; // timeWait clearTimeout(this.timer); // c timeWait this.timer = setTimeout(function () { self._ajax(val); }, this.options.timeWait); }, _ajax: function (val) { var self = this; this.$target.trigger('query_start');// this.def = $.get(this.options.url, { val: val }) // .done(function (data) { self.$target.trigger('query_data', [data]); }) .error(function (data) { self.$target.trigger('query_error', [data]); }) // .always(function () { self.$target.trigger('query_end'); }); // } }; $.fn.query = function () { this.each(function () { $(this) .data("query", new query(this)) // .bind('keyup', function (e) { // $(this).data("query").handler($(this).val()); }); }); return this; }; } (jQuery));
public ActionResult Load(string val) { var data = new { id=1, name="..."}; // . val //throw new Exception("Error", null); //System.Threading.Thread.Sleep(1000); return Json(data, JsonRequestBehavior.AllowGet); }
Source: https://habr.com/ru/post/140225/
All Articles