gpt4 book ai didi

jquery - 选择一个元素时禁用其他组

转载 作者:行者123 更新时间:2023-11-28 07:50:51 24 4
gpt4 key购买 nike

当您选择一个元素时,我正在寻找一种方法来禁用 magicsuggest 中的其他组。

来自 magicsuggest 页面的示例 ( http://nicolasbize.com/magicsuggest/data.json ):当我从“美国”组中选择一个元素时,比方说“芝加哥”,它将禁用另一个组“法国”中的元素,甚至不再显示它们。

提前致谢

最佳答案

好的,因为这现在是谷歌上的最高结果(对于“magicsuggest disable group”),这是我的解决方案:
我在“magisuggest.js”中为以下几行扩展了 API:
在默认列表(第 22++ 行)中,我添加了 2 个变量:

safeArray: "",
removeInactiveGroups: false,

第一个是存储原始数据的变量
第二个在设置为 true 时触发事件

然后你必须转到函数“_onComboItemSelected”并在末尾添加一个新行:

handlers._disableUnusedGroups();

在下面我写了新函数:

    _disableUnusedGroups: function(){
if(cfg.removeInactiveGroups){
var list_ = ms.getData();
if(cfg.safeArray==""){
cfg.safeArray=list_;
}
var test2 = JSON.stringify(ms.getSelection());
test2 = test2.split(":");
test2 = test2[test2.length-1];
var res = test2.substr(1,test2.length-4);
var testarray = [];
$.each(list_, function(a,b){
//WILDCARD = the name of the group, in the example above it would
//be "country"
if(b[WILDCARD]==res){
testarray.push(b);
}
});
ms.setData(testarray);
}
},

重要的是,带有组的名称是 json 对象中的最后一个值! (除了上面显示的,我没有找到另一种获取所选元素组名称的方法)
这添加了用于将新元素添加到所选列表的处理程序
现在你只需要扩展处理程序“_onTagTriggerClick”,当一个元素被删除时触发的最后几行:

if(ms.getSelection()["length"] == 0 && cfg.removeInactiveGroups){
ms.setData(cfg.safeArray);
}

这只会恢复选择容器中的原始列表

好的,这现在添加了处理程序来删除和添加元素,现在我们必须扩展在程序开始时触发的处理程序(如果设置了值):
处理程序以
开头“如果(元素!== null){”
该函数现在像这样寻找我:

if(element !== null) {
self._render(element);
if(cfg.value!==null){
var check = cfg.value[0];
var list_ = ms.getData();
var searchItem;
var newList_ = [];

if(cfg.safeArray==""){cfg.safeArray=list_;}

$.each(list_, function(a,b){
//WILDCARD2 = the name of the elements, in the example above it
//would be "name"
if($.trim(b[WILDCARD2]) == check){
//WILDCARD = the name of the group, in the example above it would
//be "country"
searchItem=b[WILDCARD];
}
});

$.each(list_, function(a,b){
if(b[WILDCARD]==searchItem){
newList_.push(b);
}
})
ms.setData(newList_);
}
}

现在你必须添加一行

removeInactiveGroups: true,

在您自己的 javscript 中设置配置选项
它并不完美,因为您必须对一些值进行硬编码,但是您可以为通配符添加一个新的全局默认变量,它应该可以工作

希望这对以后登陆这里的人有帮助。

关于jquery - 选择一个元素时禁用其他组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30428549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com