问题
I have three drop down lists whose values are been copied to <h:inputHidden>
components by the following JavaScript function:
function getBirthDate() {
var months = document.getElementById("months")
var hidden1 = document.getElementById("formsignup:monthField");
hidden1.value = months.options[months.selectedIndex].text;
var days = document.getElementById("days");
var hidden2 = document.getElementById("formsignup:dayField");
hidden2.value = days.options[days.selectedIndex].value;
var years = document.getElementById("years");
var hidden3 = document.getElementById("formsignup:yearField");
hidden3.value = years.options[years.selectedIndex].value;
}
Here are the three <h:inputHidden>
components:
<h:inputHidden value="#{signupBean.month}" id="monthField"/>
<h:inputHidden value="#{signupBean.day}" id="dayField"/>
<h:inputHidden value="#{signupBean.year}" id="yearField"/>
This is the command button that is supposed to invoke the function that copies the values to the inputs and then submits them to the backing bean.
<h:commandButton image="images/images/signup1.png"
styleClass="joinnow"
id="joinus" action="#{signupBean.save}"
onclick="getBirthDate()" />
But they arrive as null
in the backing bean. How is this caused and how can I solve it?
Edit: jQuery freezes when I try to select elements from JSF components. Is there a problem with the IDs of the elements?
Edit: below is a screenshot of the variables sent along with the HTTP request which proves that the values are properly been sent. What would be the problem with the bean?
回答1:
can you check the scope of your bean and if it is getting reintialized during your submit.. in that case the hidden fields gets its initial values no matter if the values are being sent from client side.
回答2:
You can check the object is present or not by using alert statement. Example
function getBirthDate() {
var months = document.getElementById("months")
var hidden1 = document.getElementById("formsignup:monthField");
alert(months);alert(months.value);
alert(hidden1);alert(hidden1.value);
If the Object value is null the Id is you given to get the element is not proper.. also check the value you are getting from the object.. I think it may help
来源:https://stackoverflow.com/questions/11182875/backing-bean-not-getting-values-sent-by-javascript