繁体   English   中英

表单验证清除JavaScript字段

[英]Form validation clears JavaScript field

我正在使用基于SEAM的JSF Web应用程序。 我有一个简单的表单,其中包含一个查找(弹出窗口),用于检索类别名称和ID(两者都检索没有问题,并存储在myView中)。 当查找返回时,类别名称用于填充“selectedCategoryName”(使用Jquery)。

当您尝试提交表单时出现问题,另一个字段未通过验证(缺少简单的必填字段)。 我的所有其他字段保持不变,但selectedCategoryName被清除(即使bean仍然具有它的值,因此修复验证错误并重新提交解析为正确的提交)。 所以我的问题是,当表单验证失败时,如何保持显示“selectedCategoryName”中的值?

提前致谢!

<s:decorate id="selectedCategoryField" template="/layout/edit.xhtml">
<span>
    <h:panelGroup rendered="#{! myView.persisted}">
    <img class="lookup" src="${request.contextPath}/images/lookup.gif" width="15" height="14" alt="" 
                        title="Category Lookup" 
                        onclick="return openNewWindow('${CategoryLookupUrl}?#{myView.lookupParameters}', 'id');"/>
    </h:panelGroup>

            <h:inputText id="selectedCategoryName" required="true" 
                        value="#{myView.selectedCategoryName}" 
                        size="50" 
                        readonly="true">

            <a:support event="onchanged" reRender="selectedCategoryField" ajaxSingle="true"/>
    <a:support event="oninputchange" reRender="selectedCategoryField" ajaxSingle="true"/>
            </h:inputText>
    </span>

问题是,由于readonly="true" ,实际上bean没有保持选定的值。 将该属性应用于UIInput时,将不会在提交上处理UIInput。

要解决此问题,您可以执行以下操作:
如果您使用JSF2,则可以使用readonly="#{facesContext.renderResponse}"代替。
如果没有, isReadonly在您的支持bean上定义一个方法isReadonly并使用readonly="#{myView.isReadonly}"

public boolean isReadonly() {
    return FacesContext.getCurrentInstance().getRenderResponse();
}

看一下下面这个类似的问题,尤其是答案,了解更多有关其工作原理的详细信息: 单击命令按钮时<h:inputText readonly =“true”>中的数据消失

暂无
暂无

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

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