0

How can I use the value of text field which I enter from GUI again for further computation? I have following piece of code

<tr>
  <td> Enter Index Value:</td>
  <td><input type="text" title="Enter Index#" id="ind" name="index"
             size="2" maxlength="2" /></td>
  <td><input type="text" name="bid" value=<%= lm.book_ids.get(a-1)%>
             style="visibility: hidden" /></td>
  <td><input type="text" name="brid" value=<%= lm.branch_id.get(a-1)%>
             style="visibility:hidden" /></td>
  <td><input type="text" name="cardno" value=<%= lm.cards.get(a-1)%>
             style="visibility: hidden" /></td>
</tr>

I want to use the value of text field in place of 'a' which is an arbitrary java int variable.

t0mppa
  • 3,983
  • 5
  • 37
  • 48
user3011373
  • 17
  • 1
  • 5
  • What is it that you exactly want to do? Update those hidden text fields (that should be hidden fields, if you keep them hidden, not text fields) with data of same index as your user is giving on the input field above? – t0mppa Nov 27 '13 at 17:56
  • Yes exactly. I am redirecting these values to my servlet for further computations. They have been kept hidden since I want to use their value only. – user3011373 Nov 27 '13 at 17:58

1 Answers1

0

You cannot change the variables that way. Your scriptlets (which are bad design BTW as well) will only be run once when the JSP file is rendered into HTML. When the user has loaded the page, there's only the data given by the scriptlets left as text amidst HTML.

You'd either have to save the entire data structures into a Javascript array on page load and keep swapping the data based on index or then use AJAX to fetch new data from your server.

* EDIT *

Here's an example:

<head>
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

     <script type="text/javascript">
        $(document).ready(function() {
            var indexField = $("#ind");
            var bidArray = new Array();
            bidArray[0] = '<%= lm.book_ids.get(0) %>';
            bidArray[1] = '<%= lm.book_ids.get(1) %>';

            indexField.keyup(function() {
                var index = indexField.val();
                var bid = bidArray[index];

                if (bid !== undefined) {
                    $("#bid").val(bid);
                }
            });
        });
    </script>
</head>

[...]

<tr>
    <td>Enter index value:</td>
    <td><input type="text" title="Enter Index#" id="ind" name="index"
               size="2" maxlength="2" /></td>
    <td><input type="hidden" id="bid" name="bid" /></td>
</tr>

And a JSfiddle.

Community
  • 1
  • 1
t0mppa
  • 3,983
  • 5
  • 37
  • 48