开发者

Getting an option element's value in java

I am extremely new to javascr开发者_开发知识库ipt and I have worked on this for hours.... Figured I'd ask some experts.

I am setting the value of an option element in this javascript function:

function receiveAnswer(response) {
  var aSeats = document.getElementById("aSeats");
    aSeats.options.length = 0;// clear it out

   for (var i = 0; i < response.aSeats.length; i++) { // add the items back in
    var option = aSeats.appendChild(document.createElement("option"));
    option.value = i;
    option.appendChild(document.createTextNode(response.aSeats[i]));
  }
}

Here is the html:

<% Venue v = (Venue)session.getAttribute("currentVenue"); %>
<% List<Conceptual_Package> cpList = Conceptual_PackageDAO.getInstance().getByVenue(v.getId()); %>

What Packages do you want to see?

 <form method="post" action="ttp.actions.Sale3PackAction.action">
 <select name="packid" id="packid">
     <% for (Conceptual_Package cp: cpList) { %>
    <option value="<%=cp.getId()%>"><%=cp.getName1()%></option>
    <% } %>


 </select>

    <input type="button" value="  next  " onclick="getSeats();"/>

    </form>


<!--new-->


Available Seats:

<div>

</div>

 <select name="aSeats" size="10" id="aSeats">
 </select>


    <input type="button" value="  add  " onclick="addToCart();"/>

    <div>

    </div>


Selected Seats:
 <form method="post" action="ttp.actions.sale4Action.action">
     <select name="Seat2" size="10" id="seat2">

     </select>

<input type="button" value="  remove  " onclick="removeFromCart();"/>

    </form>

<div>

</div>

 <form method="post" action="ttp.actions.finalizeSaleAction.action">

    <input type="submit" value="  Buy Tickets  "/>

    </form>

I want to get the value of those option elements in this java class:

public class finalizeSaleAction implements Action {
    public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {

        HttpSession session = request.getSession();
        Venue v = (Venue) session.getAttribute("currentvenue");
        Sale s = SaleDAO.getInstance().create(GUID.generate());
        if(session.getAttribute("type") == "packages"){


            Conceptual_Package cp = (Conceptual_Package) request.getAttribute("cp");

            List<Physical_Package> ppList = (List<Physical_Package>) request.getAttribute("seat2");

            Physical_Pkg_Set pps = Physical_Pkg_SetDAO.getInstance().create(GUID.generate());

            pps.setType("Phys Package Set");
            pps.setDiscount(cp.getDiscount());
            pps.setVenueID(v.getId());

            double price = 0;

            Object seatList = request.getAttribute("seat2");


Instead of

request.getAttribute("seat2");

use

request.getParameter("seat2");

And to get all the values of the selected option elements from browser.

Change the following select tag to

 <select name="aSeats" size="10" id="aSeats"  multiple="multiple"/>

this way , you can select multiple options values in the browser .And in the server side , you can retreive the values of the selected options like below

String[] selectedSeats = request.getParameterValues("seat2");

To get all the values of select options do the following using jQuery

 <input type ="hidden" name ="selectedSeats" id ="selectedSeats" value=""/>

 var optionValues= new Array();
 $("#aSeats").each(function()
 {
     optionValues.push($(this).val()); 
 });

 var selectedValues = optionValues.get().join(',');
 $('#selectedSeats').val(selectedValues);

From the server side you use.

String seats= request.getParameter("selectedSeats");
String selectedSeats[] = seats.split(",");


You need to set the name of the option element. When the form is submitted the browser will send name=value in the query or post data.

From your servlet code, you can get the value for that form element with:

request.getParameter(name);

You may see an IE bug that makes this complicated. See:

http://easy-reader.net/archives/2005/09/02/death-to-bad-dom-implementations/

edit:

If I understand you correctly, your form (which you would build with JS or maybe server-side) might look something like:

<html>
 <body>
  <form action="blah" method="post">
   <table>
    <tr><td>Pink Unicorn</td><td>14.95</td></tr>
    <tr><td>Sparkly Vampire</td><td>12.99</td></tr>
    <tr><td colspan="2"><input type="submit" value="do it now" /></td></tr>
   </table>
   <input type="hidden" name="item1" value="PU1" />
   <input type="hidden" name="item2" value="SV1" />
  </form>
 </body>
</html>

Note that the <td>s are visible in the browser, while the <input>s are not. The post data from this request would look like:

item1=PU1&item2=SV1
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜