jquery - 预填充 jquery token 输入文本框

标签 jquery jquery-tokeninput

在页面加载时我有一个 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/

相关文章:

jquery - 我可以使用 Jquery UI 进入 Konva (Kineticjs) 阶段并保存到 JSON 字符串吗?

javascript - 我的代码在 &lt;script&gt; 标记中工作,但在外部文件中不起作用

javascript - 在 div 中显示多个选择框和单选按钮值

javascript - jquery tokeninput 不在 h :inputtext while editing 上显示数据

javascript - 如何使用javascript从图像URl获取图像名称

javascript - 无法在 IE8 中使用 JQuery 上传文件

ruby-on-rails - jQuery token 输入失败 : "term" is undefined?

javascript - Tokeninput将新标签保存到数据库

javascript - token 输入可滚动下拉列表中的键盘导航

jQuery token 输入 : readonly not working