日期:2014-05-16  浏览次数:20310 次

求一个JS
问题是这样的:
表格第一行   是4个单选框。
选择第1个     就会在第2行   显示1个文本框
选择第2个     就会在第2行   显示2个文本框
选择第3个     就会在第2行   显示3个文本框
选择第4个     就会在第2行   显示4个文本框

(要求4个文本框的name不一样     分别为:a1、a2、a3、a4)
谁能发给完整的JS给俺啊!



------解决方案--------------------
不建议使用 name 属性,而建议使用 id 属性!

简单修改了一下,L@_@K

function createTextBox()
{
var numCount = parseInt(this.value);
var colChildren = ocontainer.childNodes;

if (numCount > = colChildren.length)
{
for (var i=colChildren.length; i <numCount; i++)
{
var oRadio = document.createElement( "input ");
oRadio.type = "text ";
// 添加 Id
oRadio.id = "txt " + i;
oRadio.value = oRadio.id;
ocontainer.appendChild(oRadio);
}
}
else
{
for (var i=colChildren.length-1; i> numCount-1; i--)
{
ocontainer.removeChild(colChildren[i]);
}
}
};
------解决方案--------------------
这下楼主满意了没?

<!doctype html public "-//w3c//dtd html 4.0 transitional//en ">
<html>
<head>
<title> new document </title>
<style type= "text/css ">
td{ border:1px solid black; }

</style>
<script type= "text/javascript ">
<!--
function nameRule(idx){
return "a " + idx;
}
function createInput(idx){
var frag = document.createElement( "fragelement ");
for(var i=1;i <=idx;i++){
var input = document.createElement( "INPUT ");
input.type= "text ";
input.id = nameRule(i);
input.name = nameRule(i);
input.value = "name: " + input.name;
frag.appendChild(input);
}
return frag;
}
function doClk(idx){
var tar = document.getElementById( "tabTest ").rows[1].cells[0];
tar.innerHTML = " ";
tar.appendChild(createInput(idx));
}
//-->
</script>
</head>

<body>
<table id= "tabTest " style= "border:1px solid black; width:300px; ">
<tr>
<td>
<input type= "radio " name= "rdoAddText " onclick= "doClk(1) "/> 1&nbsp;
<input type= "radio " name= "rdoAddText " onclick= "doClk(2) "/> 2&nbsp;
<input type= "radio " name= "rdoAddText " onclick= "doClk(3) "/> 3&nbsp;
<input type= "radio " name= "rdoAddText " onclick= "doClk(4) "/> 4
</td>
</tr>
<tr>
<td> &nbsp; </td>
</tr>
</table> </body>
</html>