【发布时间】:2015-11-19 00:22:27
【问题描述】:
我正在将一个数组列表从控制器传递到这个 homepage.jsp。我正在使用 jstl 标签显示值。
<c:forEach items="${orgList}" var="item">
<tr>
<td>
<input type="radio" class="rbutton" name="chooseOrg" value="${item.orgId}">   
<a href="http://localhost:8080/SpringDemo/deptPage/${item.orgId}">
<c:out value="${item.orgName}" /></a>
</td>
<td>
<c:out value="${item.orgDesc}" />
</td>
</tr>
这是我正在迭代的代码。它显示得非常好。我想要这些值在 javascript 中的其他地方。这是我迄今为止尝试过的:
var radioButtons = $("#orgDisplayForm input:radio[name='chooseOrg']");
var totalFound = radioButtons.length;
var checkedRadioButton = radioButtons.filter(':checked');
var selectedIndex = radioButtons.index(checkedRadioButton);
selectedindex 为我提供了 arraylist 的正确索引。我使用警报检查了它。但是下面的警报总是给出 arrayList orgList 中的第一个值。
alert("${orgList.get(selectedIndex).orgName}");
$("#updateName").val("${orgList.get(selectedIndex).orgName}");
我的动机是获取 selectedIndex 并从 arralist orgList 中获取相应的 orgname,并将其设置为 id 为“updateName”的输入字段。期待提供帮助。
整个参考代码是:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style>
html,body {
width: 100%;
}
#insertPopUp,#updatePopUp {
display:none;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
/* $("#insertPopUp").hide(); in css its display had to be made none*/
$("#insert").click(function(){
$("#insertPopUp").dialog({
autoOpen:false,
show: 'slide',
resizable: false,
position:"relative",
stack: true,
height: 'auto',
width: 'auto',
modal:true
});
$("#insertPopUp").dialog("open");
});
$("#popUpSave").click(function(){
$.ajax({
type : 'POST',
url : "http://localhost:8080/SpringDemo/insertOrganization/"
+"?insertName="+$("#insertName").val()
+"&insertDescription="+$("#insertDescription").val(),
success : function(){
$("#insertPopUp").dialog("close");
window.location.href="http://localhost:8080/SpringDemo/homePage";
}
});
});
$("#delete").click(function(){
//to get the index of the selected option in a radio button group
/* var radioButtons = $("#orgDisplayForm input:radio[name='chooseOrg']");
// this should contain the count of all your radio buttons
var totalFound = radioButtons.length;
// this should contain the checked one
var checkedRadioButton = radioButtons.filter(':checked');
// this should get the index of the found radio button based on the list of all
var selectedIndex = radioButtons.index(checkedRadioButton);
alert(selectedIndex); */
// to get value of the selected option in a radio button group
var orgid = $("#orgDisplayForm input[name='chooseOrg']:checked").val();
$.ajax({
type : 'POST',
url : "http://localhost:8080/SpringDemo/deleteOrganization/" +"?orgid="+orgid,
success : function(){
window.location.href="http://localhost:8080/SpringDemo/homePage";
}
});
});
$("#update").click(function(){
debugger;
var orgid = $("#orgDisplayForm input[name='chooseOrg']:checked").val();
$("#updatePopUp").dialog({
autoOpen:false,
show: 'slide',
resizable: false,
position:"relative",
stack: true,
height: 'auto',
width: 'auto',
modal:true
});
$("#updatePopUp").dialog("open");
var radioButtons = $("#orgDisplayForm input:radio[name='chooseOrg']");
// this should contain the count of all your radio buttons
var totalFound = radioButtons.length;
// this should contain the checked one
var checkedRadioButton = radioButtons.filter(':checked');
// this should get the index of the found radio button based on the list of all
var selectedIndex = radioButtons.index(checkedRadioButton);
/* alert("${selectedIndex}"); */
/* var name = ${orgList};
<c:out value="${colors[0]}"/> */
/* $("#updateName").text($orgList[selectedIndex].orgName); */
/* alert("<c:out value="${orgList[0].orgName}"/>"); */
/* $("#updateName").text("<c:out value="${orgList[0].orgName}"/>"); */
/* alert("<c:out value="${orgList.get(selectedIndex).orgName}"/>");
alert("${orgList.get(selectedIndex).orgName}"); */
//$("#updateName").val("${orgList.get(selectedIndex)}");
$("#updateName").val("${orgList.get(selectedIndex)}");
<%-- alert("${orgList.get(2).orgName}");
$("#updateName").text(<%=${orgList.get(i)}%>) --%>;
/* $("#updateDescription").text(${orgList[selectedIndex].orgDesc}); */
});
$("#popUpUpdate").click(function(){
var orgid = $("#orgDisplayForm input[name='chooseOrg']:checked").val();
$.ajax({
type : 'POST',
url : "http://localhost:8080/SpringDemo/updateOrganization/"
+"?updateName="+$("#updateName").val()
+"&updateDescription="+$("#updateDescription").val()
+"&orgid="+orgid,
success : function(){
$("#updatePopUp").dialog("close");
window.location.href="http://localhost:8080/SpringDemo/homePage";
}
});
});
});
</script>
</head>
<body>
<br>
<br>
<br>
<form:form id="orgDisplayForm">
<table align="center" width="60%">
<col width="30%">
<col width="30%">
<tr>
<th align="left">Organization Name</th>
<th align="left">Description</th>
</tr>
<c:forEach items="${orgList}" var="item">
<tr>
<td>
<input type="radio" class="rbutton" name="chooseOrg" value="${item.orgId}">
   
<%-- <a id ="deptAnchor" onclick=clickOrg(${item.orgId}) href="http://localhost:8080/SpringDemo/deptPage/"+${item.orgId}> --%>
<a href="http://localhost:8080/SpringDemo/deptPage/${item.orgId}">
<c:out value="${item.orgName}" /></a>
</td>
<td><c:out value="${item.orgDesc}" /></td>
</tr>
<tr><td></td><td></td></tr>
</c:forEach>
<tr><td></td><td></td></tr>
<tr><td></td><td></td></tr>
<tr><td></td><td></td></tr>
<tr>
<td><button type="button" id="insert">Insert</button>   <button type="button" id= "update">Update</button>   <button type="button" id="delete">Delete</button></td>
</tr>
</table>
<div id="insertPopUp">
<form:form method="POST" action="/insertOrganization">
<p><label for="name">Organization name</label><p><input type="text" name="name" id="insertName" /></p></p>
<p><label for="description">Organization description</label><p><input type="text" name="description" id="insertDescription" /></p></p>
<button type="submit" id="popUpSave">Save</button>
</form:form>
</div>
<div id="updatePopUp">
<p><label for="name">Organization name</label><p><input type="text" name="name" id="updateName" /></p></p>
<p><label for="description">Organization description</label><p><input type="text" name="description" id="updateDescription" /></p></p>
<button type="submit" id="popUpUpdate">Update</button>
</div>
</form:form>
</div>
</body>
【问题讨论】:
-
您是否尝试将值保存在 javascript 中 (var list = ${orgList} ),然后您可以将其用作 list[index] (顺便说一下,我不是 jsp 专家,只是一个想法)
-
不,我想试试。会试试的。谢谢
标签: javascript jquery jsp arraylist jstl