- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚学习使用 JQuery UI 的自动完成功能,并使用它向模型的 tag_list 属性添加标签。但是,当我从选项列表中选择一个标签时,它会删除输入字段中的所有先前值。
如何将所选标签附加到输入字段中的逗号分隔标签列表而不是覆盖它?
谢谢
# Autocomlete Peice Tags
$( "#search_tag_list" ).autocomplete({
source: $( "#search_tag_list" ).data('autocomplete-source') #get url from html data attribute
});
CoffeeScript :
# Autocomlete Search Tagss
split = ( val ) ->
return val.split( /,\s*/ );
extractLast = ( term ) ->
return split( term ).pop();
extractWithoutLast = ( term ) ->
term = split( term );
term.pop();
return term;
$( "#search_tag_list" )
# don't navigate away from the field on tab when selecting an item
.bind( "keydown", ( event ) ->
if ( event.keyCode == $.ui.keyCode.TAB && $( this ).autocomplete( "instance" ).menu.active )
event.preventDefault();
)
.autocomplete({
source: ( request, response ) ->
$.getJSON( $( "#search_tag_list" ).data('autocomplete-source'),
{
term: extractLast( request.term ),
# exclude already selected values:
exclude: extractWithoutLast( request.term )
},
response
);
,
search: ->
# custom minLength
term = extractLast( this.value );
if ( term.length < 2 )
return false;
,
focus: ->
# prevent value inserted on focus
return false;
,
select: ( event, ui ) ->
terms = split( this.value );
# remove the current input
terms.pop();
# add the selected item
terms.push( ui.item.value );
# add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
});
最佳答案
您仅使用自动完成插件的默认功能。
遵循 Autocomplete widget Docs您必须将小部件设置为在自动完成字段中显示多个标签/值:
(进行了一些调整,以防止已选择的标签再次出现在搜索结果中)
PHP:
<?php
$array = array("foo", "bar", "hello", "world");
$term = trim($_GET['term']);
// filter the array:
$array = preg_grep("/$term/", $array);
// check if there's anything to exclude:
if(isset($_GET['exclude'])){
// remove already selected values:
$array = array_diff( $array, $_GET['exclude'] );
}
echo json_encode($array);
?>
脚本: ( Autocomplete widget Docs )
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
function extractWithoutLast( term ) {
var term = split( term );
term.pop();
return term;
}
$( "#search_tag_list" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB && $( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
$.getJSON( $( "#search_tag_list" ).data('autocomplete-source'), {
term: extractLast( request.term ),
// exclude already selected values:
exclude: extractWithoutLast( request.term )
}, response );
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
关于javascript - 如何阻止 JQuery UI 自动完成清除以前的条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28003771/
我是 firebase 的新手,我正在尝试分页查询。我喜欢有一个“下一个”和“上一个”按钮。我的下一个按钮工作正常,我的问题是单击上一个 引用:https://firebase.google.com/
抱歉,标题这么蹩脚,但我只是不知道该放什么,希望你能理解。另外,我不知道以前是否有人问过类似的问题,因为我不知道合适的关键字 - 因此也无法用谷歌搜索。 基本上...在查看preg_match_all
我想在 TFS 中 check out 一个检入文件的先前版本。我可以轻松获得特定文件的变更集 ID 列表,但无法弄清楚如何 checkout 以前的版本。 我目前的代码: var workspace
我想使用 @FunctionalInterface来 self 代码中的 Java 8,但我希望能够将生成的类文件与 Java 6 一起使用。我认为我应该将源版本设为 1.8 , 目标版本为 1.6
自从 versions 被删除以来,我一直无法找到安装以前版本软件的方法。命令并点击 Homebrew。我在 2008 Mac Pro (3,1) 上运行 macOS 10.14.3 (Mojave)
当我开始当前的项目时,App Store 中已经有一个应用程序。此应用程序仅适用于 iPhone。 我的第一个任务是测试和构建一个也可以在 iPod Touch 上运行的版本。 大约 3 周前,App
我在 GitHub 上有一个曾经是 fork 的 repo,但现在不是了,因为我已经删除了原始项目的任何痕迹并开始了一个同名的新项目。 但是,GitHub 仍然表示该项目是 fork 的。有什么方法可
我是一名优秀的程序员,十分优秀!