预填充jquery令牌输入文本框

时间:2013-08-13 09:18:54

标签: jquery jquery-tokeninput

在页面加载

我有一个令牌输入,即

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

        });

3 个答案:

答案 0 :(得分: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);

假设病房是所需格式的有效对象,而我正在对您的代码所做的事情进行假设。但那几乎就是你需要做的事情。

答案 1 :(得分: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

答案 2 :(得分: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
            });


});
相关问题