我正在使用Magicsuggest,我需要在模糊事件上获得选择的长度。如果我通过Enter密钥添加新选择,我的代码效果很好,但是如果我从列表中选择现有选择,则无法使用。
用例
- 如果用户通过Enter键选择建议,则长度= 1-正确
- 如果用户通过鼠标单击选择建议,则长度= 0-不正确,应为1
JSFIDDLE https://jsfiddle.net/a1ejqtae/7/
html
<form action="">
<label for="keyword">Keywords</label>
<input type="text" id="keywords">
</form>
JS
$('form input').on('blur', function(){
var selectionLength = $('form .ms-sel-item').length;
$('.selection-name').text(selectionLength);
console.log('Selection is ' + selectionLength);
if( selectionLength > 0 ){
console.log('Selection is greater than 0');
}
});
ps 有人知道这个插件发生了什么,github页面仍在运行,但是所有文档和示例的网站都已下降-http://nicolasbize.com/magics.com/magicsuggest/doc.html。谢天谢地。
i在您的JSFiddle中测试了代码,并且效果很好:
var labelName = $('label').text();
console.log('label = ' + labelName);
$('#keywords').magicSuggest({
hideTrigger: true,
placeholder: 'Start typing for keyword suggestions',
useZebraStyle: true,
data: ["banshee", "fargo", "house", "csi", "ncis"],
method: 'get'
});
var update = function (){
var selectionLength = $('form .ms-sel-item').length;
$('.selection-name').text(selectionLength);
console.log('Selection is ' + selectionLength);
}
$('form input').on('blur', function(){
update();
});
$(window).on('keyup', function(e){
if(e.keyCode === 13){
update();
$(this).blur();
}
});
$(window).on('click', function(e){
update();
$(this).blur();
});
让我知道是否有任何问题。我希望它有帮助。
$(ms).on('blur', function(c,e){
var selecteion = e.getSelection();
if(selecteion[0]){
console.log(selecteion[0]);
}
});