如何使用 MagicSearch jQuery 插件以编程方式选择值? [英] How to select value programmatically with MagicSearch jQuery Plugin?
问题描述
我正在使用这个 jQuery 插件 魔法搜索
I am using this jQuery plugin MagicSearch
如何以编程方式选择一个值(例如通过 ID)?
How can I programatically select a value (by ID for example)?
推荐答案
这就是我所拥有的.在仔细分析了它们的回调函数后,我设计了一种从搜索列表中提取 ID 和名称的方法.
This is what I have. After a careful analysis of their callback functions, I have devised a method to extract the ID and Name from my search list.
请找到我尝试的代码:
<script>
var idList="";
var nameList = "";
$(function () {
var dataSource = "[{"id":1,"name":"test"},
{"id":2,"name":"example"},{"id":3,"name":"image"},
{"id":4,"name":"election"},{"id":5,"name":"IPL"},
{"id":6,"name":"Fashion"},{"id":7,"name":"Movies"},
{"id":8,"name":"Pollution"},{"id":9,"name":"Modi"},
{"id":10,"name":"Rahul"},{"id":11,"name":"Cricket"},
{"id":12,"name":"Market"},{"id":13,"name":"TestKeyword"},
{"id":14,"name":"testkeyword1"},{"id":15,"name":"testkeyword2"},
{"id":16,"name":"testkeyword3"}]";
$('#basic').magicsearch({
dataSource: dataSource,
fields: ['name'],
id: 'id',
format: '%name%',
hidden: true,
multiple: true,
focusShow: true,
isClear: true,
multiField: 'name',
multiStyle: {
space: 5,
width: 200
},
success: function ($input, data) {
idList = idList + data.id + ',';
nameList = nameList + data.name + ',';
// alert(data.id);
return true;
},
afterDelete: function ($input, data) {
idList = idList.replace(data.id + ',', "");
nameList = nameList.replace(data.name + ',',"");
//alert(data.id);
return true;
}
});
});
小提琴链接:https://jsfiddle.net/yn0cmgjt/5/
请原谅我的界面不好.我很难获得指向该插件的 CDN 链接,并且不知何故它没有以正确的方式呈现.但是请尝试一下,我希望它可以满足您的需求.
Excuse me for the bad interface. I had a difficult time getting the CDN link to this plugin and somehow it is not rendering the correct way. But play around with this and I hope it will suffice to your needs.
我创建的两个列表(idList 和 nameList)是连接在单个逗号分隔字符串中的每个元素的 id 和 name.一旦你有了这个字符串,你就可以提取出所需的信息.
The two lists (idList and nameList) that I have created are the id and name for each element concatenated in a single comma separated string. Once you have this string, you can extract out the required information.
这篇关于如何使用 MagicSearch jQuery 插件以编程方式选择值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!