gpt4 book ai didi

java - 在java中获取选项元素的值

转载 作者:行者123 更新时间:2023-11-28 21:23:45 25 4
gpt4 key购买 nike

我对 javascript 非常陌生,我已经为此工作了几个小时......我想我应该问一些专家。

我正在此 JavaScript 函数中设置选项元素的值:

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]));
}
}

这是 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>

我想获取这个java类中那些选项元素的值:

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");

最佳答案

而不是

request.getAttribute("seat2");

使用

request.getParameter("seat2");

并从浏览器获取所选选项元素的所有值。

将以下选择标签更改为

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

这样,您可以在浏览器中选择多个选项值。并且在服务器端,您可以检索所选选项的值,如下所示

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

要获取选择选项的所有值,请使用 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);

从您使用的服务器端。

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

关于java - 在java中获取选项元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5561377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com