简体   繁体   中英

Javascript submitting all the forms on a page

I'm using jsf 1.2. When a particular jsp has more than one form with a specified id, for example when using something like below, jsf gives the form a seemingly random id.

<ui:repeat>
   <h:form id="repeatingform">
      ...

I would like submit all forms using javascript. Is there a way to do this without knowing the ids of the forms?

Submitting more than one form at once it not really possible. The problem is that each form requires its own separate request - submitting a form is basically similar to clicking a link, and you can't open all links on a page at once (you can by opening them in new tabs/windows, but that's a different matter)

If you really do want to keep each form its separate form element, you can use Aquatic's example,

var forms = document.getElementsByTagName("FORM");
for (var i=0; i<forms.length; i++) 
forms[i].submit();

but replace the code which runs submit() with code which submits the form using XMLHttpRequest. You can have multiple XMLHttpRequests running in the background.

Hmm, It won't work like that real easy. If you would use something like document.form1.submit(); it posts that specific form and all values in it. So it's no use looping through all the forms and submitting every single one . That would be the same as clicking on the submit button of each single form, resulting in each form being posted separately. The solution is to collect the values of each field in each form in a single collector form, and post the collector form. You can read (with code examples!) more about it here: http://www.codetoad.com/forum/15_24387.asp

You can access all forms on the page in the next way

var forms = document.getElementsByTagName("FORM");
for (var i=0; i<forms.length; i++) 
    forms[i].submit();

I think there are several parts of this problem. 1. Are you trying to submit multiple forms in JSF? There is a way you can seggregate components in different forms and then submit a chosen group of them if req. The outer tag may be and inside these you may have as many as you may like. (Please know t:subform is a tomahawk library; but it works well with JSF). Also Subforms can be submitted in a chosed group using t:commandButton . Notice the last paragraph on subform deocumentation that all forms with comma seperated id's may be submitted. so for example some input controls here

more controls here

whole lotta controls here

something like this will allow you to submit selective forms and validating components only for forms you wish to validate. If that is what you are really trying to do here.

But if your intent is really to submit these forms by a single piece of java script I have a small advise, javascript supports lookup of form tags dynamically in a dom tree as shown in above example by aquatic. you could traverse those ui elements also by implementing a PhaseListener which gets invoked before "RENDER_RESPONSE" phase and there traverse the ComponentUITree to get the names or list of all the UI components and then provide them to javascript by scriplet variables or $ variables that JSP 2 supports.

I could give more detailed answers or examples if you could exactly lay out your problem, as to WHY you want a common java script and do you intend to submit multiple forms parallely or you intend to submit them one at a time ( as and when requiered) but by a common piece of java script.

Parallel submission of HTML form component is really not possible until and unless u use javascript to accumulate component values from all forms and then do whole lot of manual maipulating.

Give more details on your actual intent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM