我对javascript非常陌生,我已经花了好几个小时......想想我会问一些专家。
我在这个javascript函数中设置了一个option元素的值:
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");
答案 0 :(得分:2)
而不是
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(",");
答案 1 :(得分:1)
您需要设置选项元素的名称。提交表单后,浏览器将在查询中发送name=value
或发布数据。
从servlet代码中,您可以使用以下命令获取该表单元素的值:
request.getParameter(name);
您可能会看到一个IE错误,使其变得复杂。参见:
http://easy-reader.net/archives/2005/09/02/death-to-bad-dom-implementations/
编辑:
如果我理解正确,您的表单(使用JS或服务器端构建)可能类似于:
<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>
请注意,<td>
在浏览器中可见,而<input>
则不可见。此请求的发布数据如下所示:
item1=PU1&item2=SV1