在页面加载时我有一个 token 输入,即,
$('.txtWard').tokenInput(ward, {
theme: 'facebook',
allowCustomEntry: true,
preventDuplicates: true,
tokenDelimiter: '*',
searchingText: 'Searching...'
});
但问题是,当我单击另一个按钮时,我想将项目预先填充到此 token 输入文本框中。当我使用下面的代码时,文本框会复制为另一个文本框。
$(document).ready(function () {
$('.txtWard').tokenInput(ward, {
theme: 'facebook',
allowCustomEntry: true,
preventDuplicates: true,
tokenDelimiter: '*',
searchingText: 'Searching...'
});
$('#btnGet').click(function () {
var prepopulateWards = new Array();
$.ajax({
type: "POST",
url: "FetchWard",
data: JSON.stringify({ patientNo: $('#txtPatientNo').val(), locale: 'en-US' }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
for (var j = 0; j < result.wards.length; j++) {
prepopulateWards.push({ id: result.wards[j].Code, name: result.wards[j].ward });
}
}
});
$('.txtWard').tokenInput(ward, {
theme: 'facebook',
allowCustomEntry: true,
preventDuplicates: true,
tokenDelimiter: '*',
searchingText: 'Searching...',
prePopulateFromValue: true,
prePopulate: prepopulateWards
});
});
});
最佳答案
您确实应该阅读文档。我以前从未见过这个插件,但设法在几秒钟内找到如何将 token 添加到输入的方法。
不要尝试在同一个文本框中实例化插件两次,而是使用提供的方法将 token 添加到输入框。
http://loopj.com/jquery-tokeninput/
Methods
selector.tokenInput("add", {id: x, name: y});
Add a new token to the tokeninput with id x and name y.
因此按钮的点击处理程序(如果是表单提交按钮则为提交)应该类似于:
$('.txtWard').tokenInput('add',ward);
假设 ward 是所需格式的有效对象,我当然会假设您的代码的作用。但这大致就是您需要做的。
关于jquery - 预填充 jquery token 输入文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18205154/