js编程:两个文本框 只能输数字 第二个框输完自动相加显示在后面的文本框。
第二行是两个buttonadd和del单击add添加一行,最多添加10行del删除一行,剩一行不能再删求高手赐教,如果可以用,再+100分...
第二行是两个button add 和 del
单击 add 添加一行,最多添加10行
del 删除一行,剩一行不能再删
求高手赐教,如果可以用,再+100分 展开
单击 add 添加一行,最多添加10行
del 删除一行,剩一行不能再删
求高手赐教,如果可以用,再+100分 展开
1个回答
展开全部
<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="javaScript">
var num=2;
function addRow() {
if(num>10){
alert('最多只能添加10行!');
return;
}
var row, cell;
row = mytable.insertRow();
cell = row.insertCell();
cell.innerHTML = "第"+num+"行:";
cell = row.insertCell();
cell.innerHTML = "第一个数";
cell = row.insertCell();
cell.innerHTML ="<input type='text' id='num"+num+"' name='num1' onkeydown='javascript:keynumber(this)' />";
cell = row.insertCell();
cell.innerHTML ="第二个数";
cell = row.insertCell();
cell.innerHTML ="<input type='text' name='num2' onkeydown='javascript:keynumber(this)' onblur='javascript:sum(this,"+num+")'/>";
cell = row.insertCell();
cell.innerHTML = "和:";
cell = row.insertCell();
cell.innerHTML ="<input type='text' name='sum' id='sum"+num+"'>";
num++;
}
function delRow() {
if(num>2){
mytable.deleteRow();
num--;
}
}
function keynumber(str) {
var key = event.keyCode;
if (((key < 48 && key != 45) || key > 57) && key != 46) {
event.keyCode = 0;
}
}
function sum(obj, n) {
var num1 = document.getElementById("num"+n).value;
if(num1 != null & num1!=""){
try{
var sum = Number(num1)+Number(obj.value);
document.getElementById("sum"+n).value=sum;
}catch(e){
alert('请输入有效数字!');
}
}
}
</script>
</head>
<body>
<center>
<table width="800px" align="center" cellpadding="5" cellspacing="1" bgcolor="#D6EDFF" id="mytable" style="border:1px solid">
<tr>
<td>第1行:</td>
<td>第一个数</td>
<td align="left"><input type="text" name="num1" id="num1" onkeypress="javascript:keynumber(this)"></td>
<td>第二个数</td>
<td align="left"><input type="text" onkeypress="javascript:keynumber(this)" onblur="javascript:sum(this, 1)"></td>
<td>和</td>
<td align="left"><input type="text" name="sum" id="sum1"></td>
</tr>
</table>
<br>
<button onClick="addRow();">add</button>
<button onClick="delRow();">del</button>
</center>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="javaScript">
var num=2;
function addRow() {
if(num>10){
alert('最多只能添加10行!');
return;
}
var row, cell;
row = mytable.insertRow();
cell = row.insertCell();
cell.innerHTML = "第"+num+"行:";
cell = row.insertCell();
cell.innerHTML = "第一个数";
cell = row.insertCell();
cell.innerHTML ="<input type='text' id='num"+num+"' name='num1' onkeydown='javascript:keynumber(this)' />";
cell = row.insertCell();
cell.innerHTML ="第二个数";
cell = row.insertCell();
cell.innerHTML ="<input type='text' name='num2' onkeydown='javascript:keynumber(this)' onblur='javascript:sum(this,"+num+")'/>";
cell = row.insertCell();
cell.innerHTML = "和:";
cell = row.insertCell();
cell.innerHTML ="<input type='text' name='sum' id='sum"+num+"'>";
num++;
}
function delRow() {
if(num>2){
mytable.deleteRow();
num--;
}
}
function keynumber(str) {
var key = event.keyCode;
if (((key < 48 && key != 45) || key > 57) && key != 46) {
event.keyCode = 0;
}
}
function sum(obj, n) {
var num1 = document.getElementById("num"+n).value;
if(num1 != null & num1!=""){
try{
var sum = Number(num1)+Number(obj.value);
document.getElementById("sum"+n).value=sum;
}catch(e){
alert('请输入有效数字!');
}
}
}
</script>
</head>
<body>
<center>
<table width="800px" align="center" cellpadding="5" cellspacing="1" bgcolor="#D6EDFF" id="mytable" style="border:1px solid">
<tr>
<td>第1行:</td>
<td>第一个数</td>
<td align="left"><input type="text" name="num1" id="num1" onkeypress="javascript:keynumber(this)"></td>
<td>第二个数</td>
<td align="left"><input type="text" onkeypress="javascript:keynumber(this)" onblur="javascript:sum(this, 1)"></td>
<td>和</td>
<td align="left"><input type="text" name="sum" id="sum1"></td>
</tr>
</table>
<br>
<button onClick="addRow();">add</button>
<button onClick="delRow();">del</button>
</center>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询