我認爲下面將滿足您的需求(其很難完全照搬):
有沒有簡單的方法來中止了延遲的響應,但你可以擴展預輸入as I figured out here(不修改引導的.js)
概念是捕捉,檢測如果事件是KEY_UP
或KEY_DOWN
,設置一個標誌is_browsing
,然後中止process
如果is_browsing
是真實的(也就是說,如果用戶已經擊中了KEY_UP
或KEY_DOWN
並且之後沒有其他鍵)。
擴展預輸入:
// save the original function object
var _superTypeahead = $.fn.typeahead;
// add is_browsing as a new flag
$.extend(_superTypeahead.defaults, {
is_browsing: false
});
// create a new constructor
var Typeahead = function(element, options) {
_superTypeahead.Constructor.apply(this, arguments)
}
// extend prototype and add a _super function
Typeahead.prototype = $.extend({}, _superTypeahead.Constructor.prototype, {
constructor: Typeahead
, _super: function() {
var args = $.makeArray(arguments)
// call bootstrap core
_superTypeahead.Constructor.prototype[args.shift()].apply(this, args)
}
//override typeahead original keydown
, keydown: function (e) {
this._super('keydown', e)
this.options.is_browsing = ($.inArray(e.keyCode, [40,38])>-1)
}
//override process, abort if user is browsing
, process: function (items) {
if (this.options.is_browsing) return
this._super('process', items)
}
});
// override the old initialization with the new constructor
$.fn.typeahead = $.extend(function(option) {
var args = $.makeArray(arguments),
option = args.shift()
// this is executed everytime element.modal() is called
return this.each(function() {
var $this = $(this)
var data = $this.data('typeahead'),
options = $.extend({}, _superTypeahead.defaults, $this.data(), typeof option == 'object' && option)
if (!data) {
$this.data('typeahead', (data = new Typeahead(this, options)))
}
if (typeof option == 'string') {
data[option].apply(data, args)
}
});
}, $.fn.typeahead);
此預輸入擴展可以放置在任何地方,例如在<script type="text/javascript">
-section
測試擴展:
<input type="text" id="test" name="test" placeholder="type some text" data-provide="typeahead">
<script type="text/javascript">
$(document).ready(function() {
var url='typeahead.php';
$("#test").typeahead({
items : 10,
source: function (query, process) {
return $.get(url, { query: query }, function (data) {
return process(data.options);
});
}
});
});
</script>
A「 serverside「PHP腳本返回大量隨機選擇ns強制延遲,typeahead.php:
<?
header('Content-type: application/json');
$JSON='';
sleep(3); //delay execution in 3 secs
for ($count=0;$count<30000;$count++) {
if ($JSON!='') $JSON.=',';
//create random strings
$s=str_shuffle("abcdefghijklmnopq");
$JSON.='"'.$s.'"';
}
$JSON='{ "options": ['.$JSON.'] }';
echo $JSON;
?>
它真的好像爲我工作。但我不能確定它會適用於您的情況。如果你有成功與否,讓我現在。
+1好問題,但是你能否讓你的標題更具描述性? – Marijn 2013-03-21 10:45:51
什麼版本的Bootstrap? – ZimSystem 2013-03-21 11:27:34
@Skelly Bootstrap 2.3.0 – charliexx 2013-03-21 11:31:02