<input type="submit" value="提交"/>" </form> </body> <script type="text/javascript"> var n=3; //增加一行 function add() { n++; var table=document.getElementById('tab'); var htmlRow=table.insertRow();//新增一行 var htmlCell=htmlRow.insertCell();//新增一列 htmlCell.innerHTML="问题:<input type='text' name='question'"+n+"/>"; htmlCell=htmlRow.insertCell();//新增第二列 htmlCell.innerHTML="回答:<input type='text' name='answer'"+n+"/>"; htmlCell=htmlRow.insertCell();//新增第三列 htmlCell.innerHTML="<input type='button' onclick='delete_Row(this)' value='删除'/>"; }</script> </html>
点击右侧方块可以给table里面面的text赋值,但是却无法给新增的行赋值
你的input的设置时name,你没有设置id,如何用id进行赋值的? 我表示不明白你不是新加的那些,可以赋值?并且,还有那个copy函数 var count=0; function copy(id){ count++; document.getElementById("question"+count).value=id.innerHTML; } 如果你是点击的第五个,那么这个地方的id.innerHTML应该是“这是第5个问题”,那么因为是第一个点击的这个,所以count=1,就会被赋值到id=question1上面去。我说的这些并没有测试,有很多接口,而且我看不出来你页面的具体效果,只是对着几个问题,觉得很怪,就问一下。
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP '2.jsp' starting page</title>
<script type="text/javascript">
//<![CDATA[
var tips; var theTop = 200/*这是默认高度,越大越往下*/; var old = theTop;
function initFloatTips() {
tips = document.getElementById('floatTips');
moveTips();
};
function moveTips() {
var tt = 50;
if (window.innerHeight) {
pos = window.pageYOffset
}
else if (document.documentElement && document.documentElement.scrollTop) {
pos = document.documentElement.scrollTop
}
else if (document.body) {
pos = document.body.scrollTop;
}
pos = pos - tips.offsetTop + theTop;
pos = tips.offsetTop + pos / 10;
if (pos < theTop) pos = theTop;
if (pos != old) {
tips.style.top = pos + "px";
tt = 10;
}
old = pos;
setTimeout(moveTips, tt);
}
//删除某一行
function delete_Row(deleteRow)
{
var table=document.getElementById("tab");
if(window.confirm("你确定要删除该行数据吗?")==true){
table.deleteRow(deleteRow.parentElement.parentElement.rowIndex);
}
}
//点击问题赋值给表格赋值
var count=0;
function copy(id){
count++;
document.getElementById("question"+count).value=id.innerHTML;
}
//!]]>
</script> <style type="text/css">
.floatTips
{
position: absolute;
border: solid 1px #777;
padding: 3px;
top: 250px;
right: 5px;
width: 30px;
height: 300px;
background: #cccccc;
color: white;
}
.showDiv
{
position: absolute;
border: solid 1px #777;
padding: 3px;
top: 250px;
right: 5px;
width: 200px;
height: 300px;
background: #cccccc;
color: white;
}
</style> <script type="text/javascript">
function FunOnmouseUp() {
var objFloatTips = $("floatTips");
var objActivityContext = $("activityContext");
objFloatTips.className = "showDiv";
objActivityContext.style.display = "";
}
function FunMouseOut() {
var objFloatTips = $("floatTips");
var objActivityContext = $("activityContext");
objFloatTips.className = "floatTips";
objActivityContext.style.display = "none";
} function $(objID) {
return document.getElementById(objID);
}
</script>
</HEAD>
<body onload="initFloatTips()">
<div id="floatTips" onmouseover="FunOnmouseUp()" onmouseout="FunMouseOut()" class="floatTips">
问题选择
<div id="activityContext" style="display: none">
<table>
<%for (int i = 0; i < 10; i++) {%>
<tr>
<td onclick="copy(this)">
这是第<%=i%>个问题
</td>
</tr>
<%}%>
</table>
</div>
</div>
<form action="testServlet" method="post">
<input type="button" value="添加" onclick="add()"/><br/>
<table id="tab">
<tr>
<td>问题:<input type="text" name="question1"/></td>
<td>回答:<input type="text" name="answer1"/></td>
<td><input type="button" onclick="delete_Row(this)" value="删除"/></td>
</tr>
<tr>
<td>问题:<input type="text" name="question2"/></td>
<td>回答:<input type="text" name="answer2"/></td>
<td><input type="button" onclick="delete_Row(this)" value="删除"/></td>
</tr>
<tr>
<td>问题:<input type="text" name="question3"/></td>
<td>回答:<input type="text" name="answer3"/></td>
<td><input type="button" onclick="delete_Row(this)" value="删除"/></td>
</tr>
</table>
<input type="submit" value="提交"/>"
</form>
</body>
<script type="text/javascript">
var n=3;
//增加一行
function add() {
n++;
var table=document.getElementById('tab');
var htmlRow=table.insertRow();//新增一行
var htmlCell=htmlRow.insertCell();//新增一列
htmlCell.innerHTML="问题:<input type='text' name='question'"+n+"/>";
htmlCell=htmlRow.insertCell();//新增第二列
htmlCell.innerHTML="回答:<input type='text' name='answer'"+n+"/>";
htmlCell=htmlRow.insertCell();//新增第三列
htmlCell.innerHTML="<input type='button' onclick='delete_Row(this)' value='删除'/>";
}</script>
</html>
我表示不明白你不是新加的那些,可以赋值?并且,还有那个copy函数
var count=0;
function copy(id){
count++;
document.getElementById("question"+count).value=id.innerHTML;
}
如果你是点击的第五个,那么这个地方的id.innerHTML应该是“这是第5个问题”,那么因为是第一个点击的这个,所以count=1,就会被赋值到id=question1上面去。我说的这些并没有测试,有很多接口,而且我看不出来你页面的具体效果,只是对着几个问题,觉得很怪,就问一下。