【问题标题】:How to reset input text values to defaults?如何将输入文本值重置为默认值?
【发布时间】:2012-04-23 22:47:52
【问题描述】:

我有一个页面,其中 a4j:commandbutton 捕获 2 个文本框的值。文本框的值默认是这样的:

 <a4j:region id="pnreg" >
  <h:inputText id="pn" binding="#{bean.pn}"
  value="Enter Number..."       
  onclick="clickclear(this, 'Enter Number...')"/>      
  </a4j:region> 
  </td>   
  <td width="20">
  <a4j:region id="qtyreg" > 
  <h:inputText value="Enter Part Qty..." id="qty" binding="#{bean.qty}"       
  onclick="clickclear(this, 'Enter Qty...')"/>      
  </a4j:region>
  </td>      
  <td width="20%">
  <a4j:commandButton id="addPart" render="pn,qty" value="Add Part"
   action="#{bean.submit}" /> 

当页面第一次加载时,文本框显示默认值,当用户点击它们时,这些值被清除为编码。单击按钮时,我希望文本框返回默认值。使用 jquery 我想我需要添加一个点击事件处理程序,但是richfaces api 已经有一个附加到 a4j:commandbutton 的事件处理程序。如何使我的 jquery 事件处理程序与richfaces 事件处理程序一起工作?谢谢。

【问题讨论】:

  • 在普通的 HTML 中使用表单和输入,这通过简单地创建一个类型为“reset”的输入来完成,它将显示为一个按钮。

标签: jquery jsf-2 richfaces


【解决方案1】:

创建一个新的 JavaScript 函数,在您的文本字段上调用 ​​clickclear()。然后修改a4j:commandButton,添加一个oncomplete="yourNewFuction();"属性,以便在您的 AJAX 调用完成时执行您的新函数。

【讨论】:

  • 谢谢,我会试一试,然后回来。
【解决方案2】:

如果你想设置回inputText的值,那么你应该在a4j:commandButtononclick方法中添加你的js函数:

<a4j:commandButton id="addPart" value="Add Part"
    onclick="alert('Before the ajax submit...');verifyInputTexts();"
    action="#{bean.submit}"
    render="pn,qty" limitToList="true"
    oncomplete="alert('Ajax completed!')" /> 

在该代码示例中,verifyInputTexts 可能是您的 JS 函数,用于使用 javascript 验证您的 HTML。还有一件事,我为 a4j:commandButton 编写属性的方式是按照它们执行的顺序,limitToList="true" 是告诉 ajax 应该只重新渲染 render 值中的组件。

【讨论】:

    【解决方案3】:

    将 inputText 放在与“取消”按钮相同的表单中并使用 type="reset" 例如:

        <h:form id="globalNotificationSettingsForm"> 
           your input <h:inputText>
           <a4j:commandButton id="cancelButton"
             ....other tags..
            render="globalNotificationSettingsForm"
            immediate="true"
            type="reset"/>
     </h:form>
    

    【讨论】:

      猜你喜欢
      • 2021-04-07
      • 1970-01-01
      • 2021-12-05
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      • 1970-01-01
      • 2012-12-22
      相关资源
      最近更新 更多