Javascript 是",;内容“;是否在创建时立即设置jquery对象?

Javascript 是",;内容“;是否在创建时立即设置jquery对象?,javascript,jquery,Javascript,Jquery,关于另一篇博文:,我想问一下,jQuery对象引用的div的内容是否在创建对象时立即设置 JSF页面: <!-- initialize script with the formId and the id of the input --> <script type="text/javascript"> $(document).ready(function(){ mx.autocomp.overlay.init('formId', 'searchVal

关于另一篇博文:,我想问一下,jQuery对象引用的div的内容是否在创建对象时立即设置

JSF页面:

<!-- initialize script with the formId and the id of the input -->
<script type="text/javascript">
    $(document).ready(function(){
        mx.autocomp.overlay.init('formId', 'searchValue');
    });
</script>

<!-- input text that at "keyup" calls on sendRemoteCommand -->
<p:inputText
    id="searchValue"
    value="#{searchBean.searchValue}"
    onkeyup="sendRemoteCommand();" />

<!-- PrimeFaces remoteCommand that executes db search -->
<!-- and present result in "searchResult" div -->
<p:remoteCommand 
    name="sendRemoteCommand" 
    actionListener="#{searchBean.complete()}" 
    update="searchResult"
    oncomplete="mx.autocomp.overlay.handleOverlay();" />

<!-- PrimeFaces overlayPanel that is displayed if the search returned a result -->
<!-- i.e. the div "searchResult" has content ($searchResult.html() has content) -->
<p:overlayPanel 
    id="overlay" 
    widgetVar="overlayWid" 
    for="formId:searchValue" 
    showEvent="none">

    <h:panelGroup layout="block" id="searchResult">

        <!-- Content will be presented here after a p:remoteCommand is finished -->

    </h:panelGroup>

</p:overlayPanel>
如上所述,“init”中的jQuery对象似乎无权访问div的内容,而在“handleOverlay”中创建的jQuery对象则无权访问div的内容

我的期望是jQuery对象的“html()”函数将实时检查内容,而不是像看上去那样从创建内容时获取旧信息。因此,我的问题是:


jQuery对象引用的div的内容是否仅在创建对象时设置?

这取决于何时调用
init()
函数。例如,如果在DOM尚未就绪时调用它,则选择为空

因此,无论何时使用
$(“#选择器”).find()
,它都会在准确的时间为您提供元素选择。如果你把它放在你的
handleOverlay()
中,它应该可以工作


试着习惯,你就不必处理类似的事情。

这是因为变量$searchResultComp是在init函数中设置的,jquery对象本身是动态的,但不是使用jquery对象的查询结果

find()方法查找与指定为find条件的模式匹配的jquery对象的所有子体,并将它们作为新的jquery对象返回。如果没有匹配的子体,它将返回一个没有内容的jquery对象。您可以通过警告对象的长度来测试它,它应该为零


因此,在handleOverlay函数中,您需要重置$searchResultComp以查找所有现在符合您条件的子体。

谢谢您的提示。当文档准备就绪时调用init()函数:$(document.ready(function(){mx.autocomp.overlay.init('formId','searchValue');});啊,对了,现在我看到您正在使用
.find(“[id$=”+inSearchValue+”])
。我猜您想说的是
.find(“#”+inSearchValue)
?脚本用于JSF页面,因此id的构建方式如下:[formId]:[SomeComponentD1]:[SomeComponentDN]:searchValue。在表单中有一个起始点,我使用.find(“[id$=”+inSearchValue+“])来获取id以该id结尾的组件。因此不可能创建jQuery变量,在它引用的对象(div)发生任何更改后,请查看这些更改?在本例中,当您在init()中设置变量$searchResultComp时jquery没有要返回的DOM内容,因为它还没有被添加,所以对象为null。然后,当您在handleOverlay中请求对象的html()时,它将返回null。感谢您的解释!我很沮丧不能理解发生了什么。
var formId;
var $searchValueComp;
var $searchResultComp;

function init(inFormId, inSearchValue){
    formId = inFormId;
    $searchValueComp = $("#"+inFormId).find("[id$="+inSearchValue+"]");
    $searchResultComp = $("#"+inFormId).find("[id$=searchResult]");
}

function handleOverlay(){
    var fn = window["overlayWid"];
    var result = document.getElementById($searchResultComp.attr("id")).innerHTML;

    if($searchValueComp.val().length==0){
        fn.hide();
    }

    // Test - This does not work as I get an empty alert
    alert($searchResultComp.html());

    // Test - This works.
    var $test = $("#"+formId).find("[id$=searchResult]");
    alert($test.html());

    // I need to check if the div: "searchResultComp" has any content. 
    // As I don't get $searchResultComp.html() to work, I'm forced to 
    // use the "getElementById" way instead. 
    if(result.length==0){
        fn.hide();
    }else{
        fn.show();
    }

}