這裏是我的代碼:dcNewForm.xhtml如何在顯示對話框時調用JavaScript函數?
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<ui:composition>
<h:panelGrid id="dcNewView">
<h:outputText value="Address" styleClass="arianeDialogLabel"/>
<p:inputTextarea id="newDCaddress" styleClass="arianeDialogInputTitle" label="Description" required="true" autoResize="false" style="resize:none" value="#{datacenterNewController.address}"/>
</h:panelGrid>
<script type="application/javascript">
var addresstxtarea = document.getElementById("createDCDialogForm:newDCaddress");
function initialize() {
// body
}
window.onload = function(){
initialize();
}
</script>
</ui:composition>
</html>
<html>
<p:dialog header="Create a new datacenter" widgetVar="dcCreateDialog">
<h:form id="createDCDialogForm">
<ui:insert name="createDatacenter" >
<ui:include src="/templates/dcNewForm.xhtml" />
</ui:insert>
<p:commandButton value="Save this datacenter" oncomplete="dcCreateDialog.hide();"/>
</h:form>
</p:dialog>
<html>
現在我呼籲的window.onload初始化方法。但是,當我呼籲從另一個對話框創建相同的對話框時,我得到TypeError:a爲空。 據我瞭解initialize()正在調用,但無法獲得document.ElementById(blahblah)。所以我只想在加載panelGrid或創建對話框時調用initialize方法。如何實現這一目標?
應該不是你的主題/標題是_How呼叫JavaScript函數onload事件?_並且你從文檔中談論onload ...?因爲所有的答案都是這樣的,但是我認爲你希望在顯示面板或對話框時使用它。 – Kukeltje
是的。我只需要在創建對話框時調用javascript方法。 – Sagar
請創建一個簡約的例子,沒有所有的谷歌地圖的東西,只是一個簡單的mcve用例,請參見[問] – Kukeltje