3

在页面加载我有一个令牌输入,即

  $('.txtWard').tokenInput(ward, {
                                        theme: 'facebook',
                                        allowCustomEntry: true,
                                        preventDuplicates: true,
                                        tokenDelimiter: '*',
                                        searchingText: 'Searching...'
                                    });

但问题是当我单击另一个按钮时,我想将项目预填充到此令牌输入文本框。当我使用下面的代码时,文本框复制为另一个文本框。

  $(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
                });
            });

        });
4

3 回答 3

4

你真的应该阅读文档。我以前从未见过这个插件,但设法在几秒钟内弄清楚如何将令牌添加到输入中。

不要尝试在同一个文本框上两次实例化插件,而是使用提供的方法将令牌添加到输入框。

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 是所需格式的有效对象,我当然会假设您的代码的作用。但这大致就是您需要做的。

于 2013-08-13T09:34:59.137 回答
3
$(document).ready(function() {
            $("#demo-input-pre-populated").tokenInput("http://shell.loopj.com/tokeninput/tvshows.php", {
                prePopulate: [
                    {id: 123, name: "Slurms MacKenzie"},
                    {id: 555, name: "Bob Hoskins"},
                    {id: 9000, name: "Kriss Akabusi"}
                ]
            });
        });

http://loopj.com/jquery-tokeninput/demo.html#pre-populated

于 2017-02-10T11:02:05.710 回答
0
If we are getting data from db with comma separated then
var prepopulateWards = new Array();

   var mail = response.d.Data.split(',');
   $(mail).each(function (index, item) {
       prepopulateWards.push({ name: item });
   });



$('#txt_Recipients').tokenInput(
   {
     //theme: "facebook",
     preventDuplicates: true,
     prePopulateFromValue: true,
     prePopulate: prepopulateWards
            });


});
于 2017-05-17T04:05:20.120 回答