我有一个PrimeFaces的SelectOneMenu对象,下面有一个面板,根据SelectOneMenu的值有多个面板条件渲染。因此,在SelectOneMenu中包含了一个PrimeFaces p:ajax请求。Ajax请求正常触发,并且面板也能够正常显示,没有任何问题。
但现在我想在更改SelectOneMenu之前添加确认对话框,以便在继续进行Ajax请求之前警告用户他们在面板中输入的任何值都将丢失。因此,我在p:ajax的onstart事件中添加了一个JavaScript函数,其中包含JavaScript confirm。问题是当用户选择“取消”按钮时,Ajax不会触发,但我的SelectOneMenu已经选择了新值。我考虑在单击事件中调用Ajax请求。但是,PrimeFaces的SelectOneMenu没有单击事件。
JSF代码:
但现在我想在更改SelectOneMenu之前添加确认对话框,以便在继续进行Ajax请求之前警告用户他们在面板中输入的任何值都将丢失。因此,我在p:ajax的onstart事件中添加了一个JavaScript函数,其中包含JavaScript confirm。问题是当用户选择“取消”按钮时,Ajax不会触发,但我的SelectOneMenu已经选择了新值。我考虑在单击事件中调用Ajax请求。但是,PrimeFaces的SelectOneMenu没有单击事件。
JSF代码:
<p:selectOneMenu id="methodOfId" value="#{cc.attrs.comboBoxValue}">
<f:selectItem itemValue="-1" itemLabel=""/>
<f:selectItems value="#{cc.attrs.comboBoxOptions}" var="methodOfId" itemValue="#{methodOfId.id}" itemLabel="#{methodOfId.name}"/>
<f:param name="cid" value="#{cc.attrs.beanConversationID}" />
<p:ajax update="dynamicPanels" process="@this" onstart="return confirmMethodOfIdChange()"/>
</p:selectOneMenu>
<p:panel id="dynamicPanels">
<ui:include src="#{cc.attrs.panelSrc}" />
</p:panel>
Javascript 代码:
function confirmMethodOfIdChange() {
var userResponse = confirm("Are you sure you want to change your selection?");
return userResponse;
}
现在我该如何更改SelectOneMenu以显示其旧值?我甚至考虑在jsf SelectOneMenu中使用f:ajax和下面的javascript代码。它会要求确认,但是当我在确认框中点击“确定”时,它不执行ajax请求。
function confirmMethodOfIdChange(data) {
alert("inside confirm");
var ajaxStatus = data.status; // Can be "begin", "success" and "complete"
if (ajaxStatus == 'begin'){
var userResponse = confirm("Are you sure you want to change your selection?");
if (userResponse) {
return true;
} else {
return false;
}
}
return true;
}