我试图在数组中保存用户输入,然后将其传递给servlet。我被困在怎么办。我搜索并尝试了不同的代码,但仍然没有运气。我不知道这个函数是错误的还是在语法上但是我已经尝试过使用其他我能想到但仍然卡住的东西。
<body>
<div id="display"></div>
<div ng-controller="DemoCtrl" ng-cloak="" class="md-inline-form" ng-app="MyApp" layout="column" layout-sm="row" style="color: black" layout-align="center center" layout-align-sm="start start" layout-fill="">
<md-content class="md-whiteframe-10dp" flex-sm="">
<md-toolbar flex="" id="materialToolbar">
<div class="md-toolbar-tools">
<span flex=""></span>
<span class="md-headline" align="center">Registration Form</span>
<span flex=""></span>
</div>
</md-toolbar>
<div layout-padding="">
<div></div>
<%
%>
<form action="" >
<input type="hidden" name="action" />
<div layout="row" layout-sm="column">
<md-input-container flex-gt-sm="">
<label>LRN:</label>
<input name="a" id="lrn" required="" type="number" placeholder="Your LRN" style="width: 160px">
</md-input-container>
<md-input-container flex-gt-sm="">
<label>Student Number:</label>
<input name="a" id="snum" required="" type="number" placeholder="Your Student Number">
</md-input-container>
</div>
<div layout="row" layout-sm="column">
<md-input-container flex-gt-sm="">
<label>First Name</label>
<input name="a" id="fn" required="" type="text" placeholder="Your First Name">
</md-input-container>
<md-input-container flex-gt-sm="">
<label>Middle Name:</label>
<input name="a" id="mn" required="" type="text" placeholder="Your Middle Name">
</md-input-container>
<md-input-container flex-gt-sm="">
<label>Last Name:</label>
<input name="a" id="ln" required="" type="text" placeholder="Your Last Name">
</md-input-container>
</div>
<div layout="row" layout-sm="column">
<p style="font-size: 12px; margin-left: 3px; margin-top: 18px;">Gender: </p>
<input type="hidden" name="a" value="{{user.gender}}" />
<md-radio-group style="margin: 12px 0 19px;" ng-model="user.gender" required="">
<md-radio-button value="Male" class="md-primary">Male</md-radio-button>
<md-radio-button value="Female" ">Female</md-radio-button>
</md-radio-group>
</div>
<div layout="row" layout-sm="column">
<md-input-container flex-gt-sm="">
<label>Parent</label>
<input required="" type="text" id="p" name="a" placeholder="Your Parent" />
</md-input-container>
</div>
<div layout="row" layout-sm="column">
<!--<md-button class="md-raised md-primary" style="width:100%; margin: 5px 5px;" type="submit" >Submit</md-button><br><br>
<md-button class="md-raised md-primary" style="width:100%; margin: 5px 5px;" type="reset" >Reset</md-button><br> -->
<input type="submit" value="Save" onclick="insert();" />
<input type="submit" value="Submit" onclick="form.action='register';" />
</div>
</form>
</div>
</md-content>
</div>
</body>
这是我的功能,当我按下阵列上的保存时,它会假设存储输入。但没有任何事情发生..
var LRN = new Array();
var SNUM =new Array();
var FN =new Array();
var MN =new Array();
var LN =new Array();
var G =new Array();
var P =new Array();
var lrnInput = document.getElementById("lrn");
var snumInput = document.getElementById("snum");
var fnInput = document.getElementById("fn");
var mnInput = document.getElementById("mn");
var lnInput = document.getElementById("ln");
var gInput = document.getElementById("g");
var pInput = document.getElementById("p");
function insert ( ) {
var lrnInput = document.getElementById("lrn").value();
var snumInput = document.getElementById("snum").value();
var fnInput = document.getElementById("fn".value());
var mnInput = document.getElementById("mn").value();
var lnInput = document.getElementById("ln").value();
var gInput = document.getElementById("g").value();
var pInput = document.getElementById("p").value();
LRN[LRN.length]=lrnInput
SNUM[SNUM.length]=snumInput
FN[FN.length]=fnInput
MN[MN.length]=mnInput
LN[LN.length]=lnInput
G[G.length]=gInput
P[P.length]=pInput;
}
每当我点击保存按钮时都没有任何反应..我只是尝试调用lrn var但我仍然不知道该怎么做
这是我的Servlet代码:
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String[] lrn = request.getParameterValues("lrn");
List<String> list = Arrays.asList(lrn);
try {
out.println("<html>");
out.println("<head>");
out.println("<title>INFORMATION</title>");
out.println("<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css'>");
out.println("<link rel='stylesheet' href='css/style1.css'>");
out.println("</head>");
out.println("<body>");
out.println("<section>");
out.println("<h1>MALE</h1>");
out.println("<div class='tbl-header'>");
out.println("<table cellpadding='0' cellspacing='0' border='0'>");
out.println("<thead>");
out.println("<tr>");
out.println("<th>LRN</th>");
out.println("<th>Student Number</th>");
out.println("<th>First Name</th>");
out.println("<th>Middle Name</th>");
out.println("<th>Last Name</th>");
out.println("<th>Gender</th>");
out.println("<th>Parent</th>");
out.println("</tr>");
out.println("</thead>");
out.println("</table>");
out.println("<div class='tbl-content'>");
out.println("<table cellpadding='0' cellspacing='0'border='0'>");
out.println("<tbody>");
for (int i = 0; i<list.size(); i++) {
if (i>0 && i%7==0) {
out.println("</tr><tr>");
}
out.println("<td>" + list.get(i) + "</td>");
}
/*
out.println("<td>"+lrn+"</td>");
out.println("<td>"+snum+"</td>");
out.println("<td>"+fn+"</td>");
out.println("<td>"+mn+"</td>");
out.println("<td>"+ln+"</td>");
out.println("<td>"+gender+"</td>");
out.println("<td>"+parent+"</td>");*/
out.println("</tbody>");
out.println("</table>");
out.println("</div>");
out.println("</section>");
out.println("<section>");
out.println("<h1>FEMALE</h1>");
out.println("<div class='tbl-header'>");
out.println("<table cellpadding='0' cellspacing='0' border='0'>");
out.println("<thead>");
out.println("<tr>");
out.println("<th>LRN</th>");
out.println("<th>Student Number</th>");
out.println("<th>First Name</th>");
out.println("<th>Middle Name</th>");
out.println("<th>Last Name</th>");
out.println("<th>Parent</th>");
out.println("</tr>");
out.println("</thead>");
out.println("</table>");
out.println("<div class='tbl-content'>");
out.println("<table cellpadding='0' cellspacing='0'border='0'>");
out.println("<tbody>");
out.println("</tbody>");
out.println("</table>");
out.println("</div>");
out.println("</section>");
out.println("<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>");
out.println("<script src='js/index1.js'></script>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
答案 0 :(得分:0)
为要在jsp文件中发送到servlet的每个值插入隐藏字段。 像
<input type="hidden" name="lrnStr" />
然后在insert()函数中,您需要编写代码以将每个lrn值附加到lrnStr隐藏表单字段中。
当您提交表单时,您将收到此字段并用逗号分隔它们并使用。
function insert ( ) {
//use jquery here
$("#lrnStr").val($("#lrnStr").val()+","+$("#lrn").val());
//same for other fields
}
在servlet代码中,您需要添加以下代码以获取值列表。
String lrnArray[] = null;
String lrnStr = request.getParameter("lrnStr");
if(lrnStr!=null){
lrnArray = lrnStr.split(",");
for (int i = 0; i<lrnArray.length; i++) {
if (i>0 && i%7==0) {
out.println("</tr><tr>");
}
out.println("<td>" + lrnArray[i] + "</td>");
}
}