如何使用MagicSearch jQuery插件以编程方式选择值? [英] How to select value programmatically with MagicSearch jQuery Plugin?
问题描述
我正在使用此jQuery插件 MagicSearch
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和名称.拥有此字符串后,您可以提取所需的信息.
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屋!