Getting selected value of a SelectOneMenu

前端 未结 1 515
感情败类
感情败类 2020-12-17 02:47

I\'m testing the component \"SelectOneMenu\" on a jsf page. I\'m populating this component dinamically though my ManageBean (that will get all Animals from database).

<
相关标签:
1条回答
  • 2020-12-17 03:10

    There are many solutions to the presented problem. I present here two basic ideas.

    1. Server-side solution. Simply attach <f:ajax> tag inside your <h:selectOneMenu> to update selected values and rerender user's choice, like in

      <h:selectOneMenu id="combo" value="#{animalsManage.animalSelected}">
          <f:selectItem itemLabel="Select..."  noSelectionOption="true"/>
          <f:selectItems value="#{animalsManage.allAnimals}" />
          <f:ajax execute="combo" render="textbox" />
      </h:selectOneMenu>
      <h:inputText id="textbox" value="#{animalsManage.animalSelected }" />
      

      If you like, you may also do some custom logic with selected element in ajax listener by specifying listener="#{animalsManage.performCustomAjaxLogic}" of <f:ajax> tag.

    2. Client-side solution. Simply update element with id="textbox" on basic change event. So, if you use jQuery the solution will be

      $('#combo').change(function() {
          $('#textbox').val($('#combo').val());
      });
      

      Thought the client-side solution will bind only text value of your input component.

    0 讨论(0)
提交回复
热议问题