本文介绍了如何访问jsp页面中动态创建的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何访问textBox中的值以将其传递到SQL数据库中。
代码如下:
<!DOCTYPE html PUBLIC - // W3C // DTD HTML 4.01 Transitional // ENhttp://www.w3.org/TR/html4/loose.dtd\">
<%@ page import =java.sql。*%>
<%@ page import =java.io. *%>
< html>
< head>< title> orderSample< / title>
< script src =http://code.jquery.com/jquery-latest.min.js>< / script>
< script>
$(document).ready(function(){
$('。add')。click(function(){
$(this).closest('tr')。find ('.quantity')。attr('disabled',!this.checked);
});
});
< / script>
< / head>
< body bgcolor =white>
< form method = post name = orderForm>
< h1> sql< / h1>中数据库'caffe'的表'item details'
System.out.println(adadadasasasasasasasasas);
int i = 0;
%>
< TABLE cellpadding =15border =1style =background-color:#ffffcc;>
< TR>
< TD> ORDER ID< / TD>
< TD> ORDER PRICE< / TD>
< TD>数量< / TD>
< TD>添加项目< / TD>
< / TR>
System.out.println(我在Ob);
%>
< TR>
< TD> asdadsas< / TD>
< TD> asdasdasd< / TD>
< td>< input type =checkboxclass =add/>< / td>
< td>< input type =textclass =quantity/>< / td>
<! - < td>< INPUT TYPE =TEXTNAME =NAME_TEXT<%= i%> VALUE =IsEnabled ={Binding ElementName = checkBox1,Path = IsChecked}/>< / td>
< td>< INPUT TYPE =TEXTNAME =NAME_TEXT<%= i%> VALUE =/>< / td>
< td><输入类型=复选框名称=其他<%= i%> onclick =enable_text(this.checked,<%= i%>)>< / td> - >
< / TR>
< / TABLE>
< / form>
< / body>
< / html>
解决方案
或者给他们所有相同的名字
< td>< input type =textname =name/>< / td>
以便您可以使用
String [] values = request.getParameterValues(name);
或者给它们全部相同的前缀
< td>< input type =textname =name $ {someDynamicValue}/>< / td>
以便您可以使用
<$ p $ (Entry< String,String []> entry:request.getParameterMap()。entrySet()){
if(entry.getKey()。startsWith(name) ){
String value = entry.getValue()[0];
//添加到列表?
$ / code $ / pre
$ hr
或者当它是一个增量后缀
< td>< input type =textname =name $ {index }/>< / td>
然后使用
for(int i = 0; i< Integer.MAX_VALUE; i ++){
String value = request.getParameter(name+ i);
if(value == null){
break;
}
//添加到列表?
}
How can I access the values in the textBox to pass it into a SQL database. What will be the text-box's unique name in each row?
The code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<html>
<head><title>orderSample</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function() {
$('.add').click(function() {
$(this).closest('tr').find('.quantity').attr('disabled', !this.checked);
});
});
</script>
</head>
<body bgcolor="white">
<form method=post name=orderForm>
<h1>Data from the table 'item details' of database 'caffe' in sql </h1>
<%
System.out.println("adadadasasasasasasasasas");
int i = 0;
%>
<TABLE cellpadding="15" border="1" style="background-color: #ffffcc;">
<TR>
<TD>ORDER ID</TD>
<TD>ORDER PRICE</TD>
<TD>Quantity</TD>
<TD>Add Item</TD>
</TR>
<%do{
System.out.println("I am in Ob");
%>
<TR>
<TD>asdadsas</TD>
<TD>asdasdasd</TD>
<td><input type="checkbox" class="add" /></td>
<td><input type="text" class="quantity" /></td>
<!-- <td><INPUT TYPE="TEXT" NAME="NAME_TEXT<%=i%>" VALUE="" IsEnabled="{Binding ElementName=checkBox1, Path=IsChecked}"/></td>
<td><INPUT TYPE="TEXT" NAME="NAME_TEXT<%=i%>" VALUE="" /></td>
<td><input type="checkbox" name="others<%=i%>" onclick="enable_text(this.checked,<%=i%>)" ></td>-->
</TR>
<%i++; }while(i<2); %>
</TABLE>
</form>
</body>
</html>
解决方案
Either give them all the same name
<td><input type="text" name="name" /></td>
so that you can use
String[] values = request.getParameterValues("name");
Or give them all the same prefix
<td><input type="text" name="name${someDynamicValue}" /></td>
so that you can use
for (Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
if (entry.getKey().startsWith("name")) {
String value = entry.getValue()[0];
// Add to list?
}
}
Or when it is an incremental suffix
<td><input type="text" name="name${index}" /></td>
then use
for (int i = 0; i < Integer.MAX_VALUE; i++) {
String value = request.getParameter("name" + i);
if (value == null) {
break;
}
// Add to list?
}
这篇关于如何访问jsp页面中动态创建的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!