开发者

Java Script issue while submitting a form through JSP

开发者 https://www.devze.com 2023-03-23 06:10 出处:网络
I am facing an issue when I am trying to submit a JSP page . I will describe in short the scenario so that it would be clear.

I am facing an issue when I am trying to submit a JSP page .

I will describe in short the scenario so that it would be clear.

The Scenario

I have some input elements made up in a html component on JSP page.

The table has many <tr>. These <tr> have been given ids, for e.g. <tr id="1">.

Now I am trying开发者_JAVA百科 to pass a comma separated list of these tr ids to server side code or Servlet.

The comma separated list is formed with looping some logic on submit of JSP or more specifically a form.

The Problem:

When I submit the form sometimes I do not receive the comma separated values mentioned above at server side code.

This happens occasionally. Now when I put some delay through Java Script like setTimeOut() I do not face the issue.

So can anyone please help me guiding on this?

Is Java Script behavior a bit non-sequential sometimes?

Thanks in advance.


JavaScript execution is sequential as long as you do NOT use asynchronous mechanism (e.g. Timer, Ajax, etc).
So the question is how you submitted the form. I've ever written code of similar function and I never encountered the problems you mentioned.

Here I provide my solution. Hope this can help.

I don't use a <input type="submit" /> to submit my form. Instead, a common button is employed with an onclick event binding:

<form id="form1" action="serverscripturl" method="post">
  <input type="button" value="submit" onclick="form1submit()" />
  <input id="field1" name="field1" type="hidden" />
</form>

Then I use a JavaScript function to perform the concatenation and submission:

function form1submit(){
  var list = ""; // your comma separated list
  for (var i = 0; i < data.length; i ++)
    list += (data[i]+",");
  // set the value of field1 the list
  document.getElementById("field1").value = list;
  document.getElementById("form1").submit(); // submit the form<br>
}

and on server side script like response.getParameter("field1") will work.

Hope this can help you.

0

精彩评论

暂无评论...
验证码 换一张
取 消