繁体   English   中英

如何将typeahead.js(Bloodhound)添加到jQuery动态创建的字段中?

[英]How do I add typeahead.js (Bloodhound) to jQuery dynamically-created fields?

我可以在静态表单上提前加载Twitter typea。 但是,在这种情况下,我想将其应用于动态生成的字段。 即使我的复制脚本将必需的“ .typeahead”类添加到了新输入中,它也似乎无法正常工作。 实际上,静态形式被几个其他类包围,这些类不是为动态字段生成的。

我觉得我需要做更多的工作才能使动态字段像静态字段一样起作用,但是我不确定。

Javascript:

<!-- Dataset: First Names -->
var firstnames = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    limit: 5,
    prefetch: {
        url: './datasets/firstnames.json',
        filter: function(list) {
            return $.map(list, function(firstname) { return { name: firstname }; });
        }
    }
});

firstnames.initialize();

$('.typeahead').typeahead(null, {
    name: 'firstnames',
    displayKey: 'name',
    source: firstnames.ttAdapter()
});
<!-- Dataset: First Names (End) -->

<!-- Dynamic Fields -->
var count=2;
$("#add").click(function(){
    var html="<div id='div"+count+"'><input id='firstname"+count+"' type='text' class='typeahead' placeholder='Additional First Name'></input><button type='button' onclick=remover('"+count+"')>Remove</button></div>";
$("#additionalnames").append(html);

    count++;
});

function remover(which){
  $("#div"+which).remove();
}
<!-- Dynamic Fields End -->

HTML:

<p>Standard field works fine (type a letter from a to g):</p>
<input type="text" id="firstname" name="firstname" class="typeahead" placeholder="First Name">
<br /><br /><br /><br /><br /><br />
<p>Dynamic field does not:</p>
<div id="additionalnames"></div>
<button id="add" type="button">Add Another</button>

我认为JSFiddle无法处理我的json文件,因此可以在此处进行实时实现: http ://drjoeseriani.com/firstnames

可下载的版本位于这里: http : //drjoeseriani.com/firstnames.zip

谢谢你的帮助。

使用javascript创建元素而不是附加原始标记可能会很有用。 为什么呢

因为如果您使用javascript(或jQuery)创建元素,则可以将事件/插件附加到该元素,在这种情况下,它可以是预输入插件。

像这样

var input = $('<input>').attr('id', 'firstname' + count).addClass('typeahead').prop('placeholder', 'Additional First Name');

现在您有了输入元素,可以将.typehead({ ... })附加到它。

因此,您的点击事件应该是这样的。

$("#add").click(function() {
  // creating a new <div id=count></div>
  var div = $('<div>').attr('id', count);

  // creating a new <input id=count class="typeahead" placeholder=".."></input> 
  var input = $('<input>').attr('id', 'firstname' + count)
    .addClass('typeahead')
    .prop('placeholder', 'Additional First Name');

  // creating a new <button>Remove</button> (with a click event)
  var button = $('<button>').text('Remove')
    .on('click', function() {
      $(this).closest('div').remove();
    });

  div.append(input); // appending the input to the div
  div.append(button); // appending the button to div

  // attaching typeahead to the newly creating input
  input.typeahead(null, {
    name: 'firstnames',
    displayKey: 'name',
    source: firstnames.ttAdapter()
  });

  // appending our newly creating div with all the element inside to #additionalnames
  $("#additionalnames").append(div);

  // incrementing count
  count++;
});

另外,我改变了一起删除remover脚本的自由。 您可以将click事件附加到按钮并查找父div并使用.closest()将其删除

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM