【问题标题】:Reset field value from another field Primefaces从另一个字段 Primefaces 重置字段值
【发布时间】:2015-11-15 22:08:22
【问题描述】:

查看:

<p:selectOneMenu id="examId" value="#{examrequisitionsController.selected.examId}" required="true" requiredMessage="#{bundle.CreateExamrequisitionsRequiredMessage_examId}">
     <f:selectItem itemLabel="Select One" itemValue="#{null}" noSelectionOption="true" />
     <f:selectItems value="#{examsController.itemsAvailableSelectOne}" var="examIdItem" itemValue="#{examIdItem}"/>
     <p:ajax listener="#{examrequisitionsController.getExamPrice()}" update="price" />
</p:selectOneMenu>

<p:inputText id="price" widgetVar="widgetPrice" value="#{examrequisitionsController.selected.price}" title="#{bundle.CreateExamrequisitionsTitle_price}" required="true" requiredMessage="#{bundle.CreateExamrequisitionsRequiredMessage_price}" readonly="#{facesContext.currentPhaseId.ordinal eq 6}" maxlength="7">
     <f:validator validatorId="decimalValidator" />
</p:inputText>

控制器:

public void getExamPrice() {
      EntityManager em = Persistence.createEntityManagerFactory("SystemCardiologyReportsPU").createEntityManager();
      Exams exam = (Exams) em.createNamedQuery("Exams.findById").setParameter("id", this.selected.getExamId().getId()).getSingleResult();
      this.selected.setPrice(exam == null ? null : exam.getPrice());        
}

JavaScript:

$(document).on('change', '#ExamrequisitionsCreateForm\\:examId_input', function () {   
    if (this.value === '')
        jQuery('#ExamrequisitionsCreateForm\\:price').val('');    
});

如果在examId 字段中选择了“Select One”选项,我想做一些事情来重置输入“price”的值。

那个JS函数有效,但是JS函数重置字段值后,标签ajax会自动自动更新到最后一个有效值。

在Controller中会更容易,但是ajax不会进入控制器,当我选择第一个选项时,这就是我尝试使用JS的原因。

或者在用户使用 JS 选择任何其他选项后禁用“选择一个”的任何功能。

我尝试了类似的方法:

$('select [value=""]').attr('style', 'display:none');

【问题讨论】:

  • 如果解决方案有效,接受答案和/或投票会有所帮助

标签: javascript java ajax jsf primefaces


【解决方案1】:

我假设examId 是一个int。所以可以请你删除

 <f:selectItem itemLabel="Select One" itemValue="#{null}" noSelectionOption="true" />

并添加

 <f:selectItem itemLabel="Select One" itemValue="0" noSelectionOption="true" />

我认为 null 转换器不起作用,因为您的 selectOneMenu 值是 int 并且无法理解 null。可以去掉js脚本。

【讨论】:

  • 您好,感谢您的回答。不,examId 不是 int,是 Exam 的对象(这个命名是因为数据库)。无论如何,我使用&lt;f:selectItem itemLabel="Select One" itemDisabled="true" /&gt; 解决了这个问题,所以无法再次选择该选项。
  • 太棒了。下次你可以探索一个用于考试的 jsf 转换器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多