问题
The constraint I have is that my JSF + Primefaces page must include a post request in straight html:
<form id="checkout" method="post" action="/checkout">
<div id="payment-form"></div>
<input type="submit" value="Pay $10">
</form>
And I can't touch that.
On the same page, I have an inputText where the user will put their email address:
<p:inputText value="#{bean.emailAddress}"/>
When the button in the <form>
is clicked for the post request, I'd like to have the inputText submitted as well and sent to a bean on my server. How could I achieve that? Just including <p:inputText value="#{bean.emailAddress}"/>
inside the <form></form>
tag seems not to work.
回答1:
Hook a JavaScript event listener to that form which triggers a <p:remoteCommand>
processing at least the <p:inputText>
of interest.
As you're using PrimeFaces, you'll have jQuery at hands. So, this should do:
<form id="checkout" ...>
...
</form>
<h:form>
<p:inputText ... />
<p:remoteCommand name="checkout" process="@form" action="#{bean.checkout}" />
</h:form>
$(document).on("submit", "#checkout", function() {
checkout();
});
来源:https://stackoverflow.com/questions/34255920/how-to-send-an-inputtext-to-a-bean-at-the-moment-a-post-request-is-made-in-jsf