简体   繁体   English

javascript添加的textarea中需要自动完成

[英]auto-complete needed in javascript added textarea

In the code below the auto-complete is working as it is supposed to in the textarea. 在下面的代码中,自动完成功能按预期在textarea中起作用。 But it does not work in the added textarea upon click on button +procedure 但是单击按钮+程序后,它在添加的文本区域中不起作用

  1. I need the auto-complete to work on all textarea added using the button. 我需要自动完成功能才能在使用该按钮添加的所有文本区域上工作。 How can I do it? 我该怎么做?

  2. Right now the auto-complete is case sensitive. 现在,自动完成功能区分大小写。 How do I make the auto-complete to happen regardless of CASE? 无论情况如何,如何使自动完成功能发生?
    HTML: HTML:

     <div id="p_procedures"> <textarea rows=5 cols=50> </textarea> </div><a href="#" id="addProcd">+ Procedure</a> 


JS: JS:

  $('textarea').textcomplete([{
  match: /(^|\s)(\w{2,})$/,
  search: function (term, callback) {
    var words = ['google', 'facebook', 'GITHUB', 'microsoft', 'yahoo'];
    callback($.map(words, function (word) {
        return word.indexOf(term) === 0 ? word : null;
    }));
 },
replace: function (word) {
    return word + ' ';
}
}]);


 var procdDiv = $('#p_procedures');
 var i = $('#p_equipments p').size() + 2;



  $('#addProcd').on('click', function(){
        var t ='<textarea rows=5 cols=50></textarea>';
        $(t).appendTo(procdDiv);

        p++;
        return false;
    });

JSFiddle 的jsfiddle

When you add a new element: 添加新元素时:

$(t).appendTo(procdDiv);

Initializing the auto-complete plugin would be exactly like it is with any other element: 初始化自动完成插件将与其他任何元素完全一样:

$(t).textcomplete({ ...options... });

Rather than copy/paste the entire initialization, you might extract it into a function that can be invoked where needed and you'd supply that function with the target element(s). 您可以将其提取到一个可以在需要的地方调用的函数中,而不是复制/粘贴整个初始化过程,并为该函数提供目标元素。 Or even just put the plugin options in a variable: 甚至只是将插件选项放在变量中:

var textCompleteOptions = [{
  match: /(^|\s)(\w{2,})$/,
  search: function (term, callback) {
    var words = ['google', 'facebook', 'GITHUB', 'microsoft', 'yahoo'];
    callback($.map(words, function (word) {
        return word.indexOf(term) === 0 ? word : null;
    }));
  },
  replace: function (word) {
    return word + ' ';
  }
}];

Then just call it repeatedly with those options: 然后只需使用以下选项反复调用它:

$('textarea').textcomplete(textCompleteOptions);

and later... 然后...

$(t).textcomplete(textCompleteOptions);

The point, though, is that any time you want to initialize the plugin on a given textarea you need to invoke the plugin initializer. 但是,要点是,每当您要在给定的textarea上初始化插件时,都需要调用插件初始化程序。 jQuery isn't going to do that for you. jQuery不会为您做到这一点。

Solves The Case-Sensitive issue here 在这里解决区分大小写的问题

search: function (term, callback) {
var words = ['google', 'facebook', 'GITHUB', 'microsoft', 'yahoo'];
callback($.map(words, function (word) {
    return word.toLowerCase().indexOf(term.toLowerCase()) === 0 ? word : null;
}));

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

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