简体   繁体   English

jQuery UI在Ajax加载的文本框上自动完成

[英]Jquery UI autocomplete on ajax loaded textbox

<div id='toolbox'>
    <div class='placeholder'></div>
</div>

I am using a click event to load this a text box into the placeholder. 我正在使用click事件将此文本框加载到占位符中。

$('#toolbox .placeholder').load('http://scarlett-angel.com/honours-server/admin_editors/word.php');});

and this is what is being loaded into it 这就是正在加载的东西

<div class="honours-container toolbox-widget">
    <label>add word</label><input type="text" name="search_word" id="search_word">
</div>

I have previously had jquery UI autocomplete working on an element that existed when the DOM was loaded. 以前,我有jquery UI自动完成功能在加载DOM时存在的元素上工作。

$( "#word" ).autocomplete({
source: 'http://46.101.8.220/fake-server/word-search.php',
minLength: 1,
success: function (data) {
            response(data.map(function (value) {
                return {
                    'label': '<li>' + value.Id + '</li>',
                    'value': value.Value
                };  
            }));
        }   
});

I want to be able to .Load different segments of an options screen into the same placeholder area and be able to clear it out when those parts are not needed. 我希望能够将选项屏幕的不同部分加载到同一占位符区域,并能够在不需要这些部分时将其清除。 I am probably going to just create all the parts in static html and just toggle them on and off into view. 我可能会只在静态html中创建所有部分,然后将它们切换为可见状态。 But I was curious if anyone knew a work around for this? 但是我很好奇,是否有人知道可以解决这个问题?

You can put your code of initializing autocomplete into a function like this: 您可以将初始化自动完成的代码放入如下函数中:

function intilalizeAutocomplete(){
$( "#word" ).autocomplete({
source: 'http://46.101.8.220/fake-server/word-search.php',
minLength: 1,
success: function (data) {
            response(data.map(function (value) {
                return {
                    'label': '</li>' + value.Id + '</li>',
                    'value': value.Value
                };  
            }));
        }   
});
}

and then call this method every time you load text box using ajax load. 然后在每次使用ajax load加载文本框时调用此方法。

$('#toolbox .placeholder').load('http://scarlett-angel.com/honours-server/admin_editors/word.php');});
intilalizeAutocomplete();

You can also make your method parameterized to use it for any textbox by just passing the textbox id. 您还可以通过仅传递文本框ID来将方法参数化为可用于任何文本框。

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

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