【问题标题】:Form validation clears JavaScript field表单验证清除 JavaScript 字段
【发布时间】:2013-08-30 12:29:39
【问题描述】:

我正在使用基于 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>

【问题讨论】:

    标签: javascript validation jsf seam


    【解决方案1】:

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

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

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

    请查看以下类似问题,尤其是答案,了解有关其工作原理的更多详细信息: Data in <h:inputText readonly="true"> disappears when command button is clicked

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-26
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多