简体   繁体   English

如何使用jquery中的append添加的类

[英]How do I use a class that has been added by the append in jquery

HTML: HTML:

<input class="typeahead" id="jj" type="text">
<a href='#' id="a">add input</a>

i do append to add more inputs to html with class in the same class input in html JS:1: 我确实追加了在html JS:1的同一个类输入中添加更多输入到html的类:

$("#a").each(function() {
$(this).on("click", function(){
     $('#jj).append('<input class="typeahead" type="text"');
});});

this is do autocomplete for class JS:2: 这是对类JS:2的自动完成:

var substringMatcher = function(strs) {
          return function findMatches(q, cb) {
          var matches, substringRegex;
          matches = [];
          substrRegex = new RegExp(q, 'i');
          $.each(strs, function(i, str) {
          if (substrRegex.test(str)) {
          matches.push(str);
          }
          });

          cb(matches);
          };
          };
         var mmm;
         $.post('arrays.php',{},function(data){
         mmm = data;
         },"json")
         .done(function() {
            $('.typeahead').each(function(){
            $(this).typeahead({
                  hint: true,
                  highlight: true,
                  minLength: 1
                  },
                  {
                  name: 'medicinses',
                  source: substringMatcher(mmm)
           });
     });
});

how do i use class "typeahead"? 我如何使用“预先输入”类?

Autocomplete do not support event delegation. 自动完成功能不支持事件委托。 You will need to attach the autocomplete to newly added element. 您需要将自动完成功能附加到新添加的元素上。

append will add the element at last position. append将在最后位置添加元素。 You can target it using :last selector from collection of elements .ggg in #kkkk 你可以使用它的目标:last从元素的集合选择.ggg#kkkk

$('button').click(function(){
 $('#kkkk').append("<input type='text' class='ggg'/>");
 $('#kkkk .ggg:last').autocomplete({
   //BIND SOURCE HERE
 });
});
 var mmm;
$.post('diagnoses.php',{},function(data){
              mmm = data;
    },"json")
    .done(function() {
                $('.ui-widget .auto').autocomplete({
                    source: mmm
                });
        });
$('#a').each(function(){
    $(this).click(function(){
    $('<input class="auto" type="text" placeholder="שם אבחנה">').appendTo('.ui-widget');
            $('.ui-widget .auto').autocomplete({
                 source: mmm
            });
    });
});

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

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