简体   繁体   English

jQuery自动完成不适用于重新渲染的文本字段

[英]jquery autocomplete does not work for rerendered textfields

Im having a problem getting jequery autocomplete to work for rerendered textfields. 我在获取jequery自动完成功能以供重新渲染的文本字段时遇到问题。

I have a a4j:commandlink to show some more inputfields in my form, when this is clicked I reRender an outer a4j:outputpanel to reRender the contents. 我有一个a4j:commandlink在表单中显示更多输入字段,单击此按钮时,我重新渲染外部a4j:outputpanel以重新渲染内容。 But to my surprise, the jQuery autocomplete event is never fired for a reRendered textfield I have. 但是令我惊讶的是,从未为我拥有的reRendered文本字段触发jQuery自动完成事件。 It is working as expected before the reRender. 在重新渲染之前,它正在按预期工作。 The textfield gets autocomplete based on its id, from google maps like this: 文本字段会根据其ID从Google地图获取自动填充,如下所示:

jQuery(document).ready(function(){
    jQuery("#createActivityAddressInputId").autocomplete({
    //This bit uses the geocoder to fetch address values
    ...
});
}

<a4j:outputPanel id="createActivityPanel">
    ...
    <h:inputText id="#{GUIComponentIds.createActivityAddressInputId}" value="#"
        {activityRequestBean.newActivityAddress}" />
    <a4j:commandLink value="#{msg.createActivityShowmapLabel}" 
        oncomplete="onShowMapPanel()" action="#{activityRequestBean.showMapClicked}" 
        rendered="#{!activityRequestBean.showMap}" 
        reRender="createActivityMapPanel,createActivityPanel" />
    ...
</a4j:outputPanel>

I was thinking that this maybe had something to do with the autocomplete event is hooked up on document load, or am I way off here? 我当时在想,这可能与自动完成事件有关,而该事件与文档加载有关,还是我离开这里?

Thanks! 谢谢!

Yes; 是; you will need to re-attach the autocomplete by calling the same method every time you re-render. 您需要在每次重新渲染时调用相同的方法来重新附加自动完成功能。 (Presumably you would cache the options that you pass to autocomplete.) (假定您将缓存传递给自动完成的选项。)

Something like: 就像是:

var autocompleteOptions = null;
$(function ()
{
   autocompleteOptions = getFromGeocoder();
});

function onReRender()
{
   $("#createActivityAddressInputId").autocomplete(autocompleteOptions);
}

Then call onReRender whenever you rerender the control. 然后,只要您onReRender控件,就调用onReRender

You need to execute your jQuery after the element has been reRendered. 您需要在元素重新渲染后执行jQuery。 Or you can try to bind jQuery event on every element even if added to the DOM later than jQuery(document).ready(). 或者,您也可以尝试在每个元素上绑定jQuery事件,即使添加到jQuery(document).ready()之后的DOM中也是如此。

edit: changed the live method call edit: removed code example because it was just wrong 编辑:更改了实时方法调用编辑:删除了代码示例,因为它是错误的

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

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