我在我的一个文本框(typeahead.js)上使用了一个预先输入,我试图关闭标签自动完成。我在文档中找不到这个,但是也许有人知道这是否可行?
有什么建议吗?
编辑:
用于插件的代码:
myTypeAhead = $('#txtTypeAhead').typeahead({
name: 'TypeAhead',
valueKey: "Value",
remote: '/ServiceHandlers/myHandler.ashx?Method=Method&Query=%QUERY',
template: ['<p>{{Value}}</p>'],
engine: Hogan
});
companyCodeTypeAhead.on('typeahead:initialized', function (evt)
{
console.log('INIT ' + $(evt.target).val());
});
companyCodeTypeAhead.on('typeahead:opened', function (evt)
{
console.log('OPEN ' + $(evt.target).val());
});
companyCodeTypeAhead.on('typeahead:closed', function (evt)
{
console.log('CLOSE ' + $(evt.target).val());
});
companyCodeTypeAhead.on('typeahead:selected', function (evt, data)
{
console.log('SELECT data==>' + data.Key + ' - ' + data.Value); //selected datum object
});
companyCodeTypeAhead.on('typeahead:autocompleted', function (evt, data)
{
console.log('AUTOCOMPLETE data==>' + data.Key + ' - ' + data.Value); //selected datum object
});
答案 0 :(得分:2)
在typeahead.js中,按住ctrl -f:.on("tabKeyed leftKeyed rightKeyed", this._autocomplete);
从关键事件处理程序列表中删除tabKeyed
。
答案 1 :(得分:1)
在typeahead 0.11.1中,这是12/9/15发布的版本:
找到以下功能并注释掉自动完成行,如图所示
并添加下一行,它模拟导致正确事件被抛出的转义键行为,但需要$e.preventDefault();
以防止浏览器绕过typeahead的逻辑来截取tab键。
_onTabKeyed: function onTabKeyed(type, $e) {
var $selectable;
if ($selectable = this.menu.getActiveSelectable()) {
this.select($selectable) && $e.preventDefault();
} else if ($selectable = this.menu.getTopSelectable()) {
// this.autocomplete($selectable) && $e.preventDefault(); // <-- Comment out
this.close() && $e.preventDefault(); // <--- Add this
}
},
希望他们能够在即将发布的版本中进行配置。
答案 2 :(得分:1)
我想要同样的东西,自动完成但是选项卡不能选择列表中的第一个选项。如果不破解实际的包装使其不易维护,这是不可能的。所以我走了一条不同的路线并添加了一个自定义数据集,它返回当前查询作为第一个结果......
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substrRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
// the typeahead jQuery plugin expects suggestions to a
// JavaScript object, refer to typeahead docs for more info
matches.push({
value: str
});
}
});
cb(matches);
};
};
var getLocalSource = function() {
return function findMatches(query, callback) {
return callback([{
'data': query
}]);
}
};
var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];
var countries = ["Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegowina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Congo, the Democratic Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia (Hrvatska)", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", "Finland", "France", "France Metropolitan", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard and Mc Donald Islands", "Holy See (Vatican City State)", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan", "Lao, People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia, The Former Yugoslav Republic of", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia (Slovak Republic)", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "Spain", "Sri Lanka", "St. Helena", "St. Pierre and Miquelon", "Sudan", "Suriname", "Svalbard and Jan Mayen Islands", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan, Province of China", "Tajikistan", "Tanzania, United Republic of", "Thailand", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "Virgin Islands (British)", "Virgin Islands (U.S.)", "Wallis and Futuna Islands", "Western Sahara", "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"];
$('#the-basics .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
}, {
name: 'local-data',
display: 'data',
source: getLocalSource()
}, {
name: 'states',
displayKey: 'value',
source: substringMatcher(states),
templates: {
header: '<div class="head">States</div>'
}
}, {
name: 'countries',
displayKey: 'value',
source: substringMatcher(countries),
templates: {
header: '<div class="head">Countries</div>'
}
});
.tt-dataset .head {
padding: 15px;
font-weight: bold;
background-color: lightgray;
}
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>
<link href="https://twitter.github.io/typeahead.js/css/examples.css" rel="stylesheet" />
<div id="the-basics">
<input class="typeahead" type="text" placeholder="Locations">
</div>
答案 3 :(得分:1)
我们在输入字段上挂起处理程序,然后再调用 typeahead
var tt = null; // init variable typeahead
var input_element = $(input);
var p = {
hint: false,
highlight: true,
minLength: 1
};
var ds = {
name: "namecompleter",
async: true,
limit: 10,
source: function (q, syncProcess, asyncProcess) { ... }
};
// The first element in the drop-down list is not selected if the [Tab] button on the keyboard is pressed
// Hook [onTabKeyed] typeahead
var disableOnTabKeyed = true; // default behavior keyTab
input_element.bind("keydown", function(e) {
// if select from the drop-down list enable [OnTabKeyed] typeahead
if( (e.keyCode == 40) || (e.keyCode == 38) ){ // (40 - keyDown) || (38 - keyUp)
disableOnTabKeyed = false;
}else if( e.keyCode != 9 ){
disableOnTabKeyed = true;
}
if( disableOnTabKeyed && (e.keyCode == 9) ){ // (9 - keyTab)
tt.typeahead("close");
//e.keyCode = 0;
//e.preventDefault();
//e.stopPropagation();
}
});
tt = input_element.typeahead(p, ds);
答案 4 :(得分:0)
要禁用TAB上的第一项选择,请在“ typeahead.jquery.js”文件中,
找到函数“ _onTabKeyed”并重写该函数。注释/删除使用$ selectable自动完成的部分,并将空字符串传递给函数autocomplete(),如下所示:
_onTabKeyed: function onTabKeyed(type, $e) {
var $selectable;
if ($selectable = this.menu.getActiveSelectable()) {
this.select($selectable) && $e.preventDefault();
} else if ($selectable = this.menu.getTopSelectable()) {
// this.autocomplete($selectable) && $e.preventDefault();
this.autocomplete('') && $e.preventDefault();
}
}