![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
js动态删除select下拉列表问题
对js不太熟悉,写了一个动态添加下拉列表的代码,但是还想实现动态删除的功能,不知道怎么写,请高手帮忙。代码:<scriptlanguage="javascript"><!...
对js不太熟悉,写了一个动态添加下拉列表的代码,但是还想实现动态删除的功能,不知道怎么写,请高手帮忙。代码:
<script language="javascript">
<!--
var num;'用于标识添加了几个select,在后面的页面用到
num=1;
var objPad;
function addSelect(str)
{
objPad = document.getElementById("pad");
if(str=="del"){
num=1;
objPad.RemoveAll( );
}
var s= new Array();
s=str.split(",")
var sel = "<select name='ksbj"+num+"'>"
for(var i=0;i<s.length;i++)
{
sel = sel+"<option value='"+s[i]+"'>"+s[i]+"</option>";
}
sel=sel+"</select><br />"
objPad.innerHTML = objPad.innerHTML+sel;
num = num+1;
}
function returnnum()
{
num1=num-1
var objPad = document.getElementById("pad");
sel="<input type='hidden' name='cs' value='"+num1+"' />"
objPad.innerHTML = objPad.innerHTML+sel;
}
-->
</script>
<input type="button" value="添加班级" onClick="addSelect('1,2,3')" />
<input type="button" value="删除班级" onClick="addSelect('del')" />
<div id="pad">
</div>
<input type="submit" name="Submit" value="提 交" onclick="returnnum()"/>
在线等,解决加分! 展开
<script language="javascript">
<!--
var num;'用于标识添加了几个select,在后面的页面用到
num=1;
var objPad;
function addSelect(str)
{
objPad = document.getElementById("pad");
if(str=="del"){
num=1;
objPad.RemoveAll( );
}
var s= new Array();
s=str.split(",")
var sel = "<select name='ksbj"+num+"'>"
for(var i=0;i<s.length;i++)
{
sel = sel+"<option value='"+s[i]+"'>"+s[i]+"</option>";
}
sel=sel+"</select><br />"
objPad.innerHTML = objPad.innerHTML+sel;
num = num+1;
}
function returnnum()
{
num1=num-1
var objPad = document.getElementById("pad");
sel="<input type='hidden' name='cs' value='"+num1+"' />"
objPad.innerHTML = objPad.innerHTML+sel;
}
-->
</script>
<input type="button" value="添加班级" onClick="addSelect('1,2,3')" />
<input type="button" value="删除班级" onClick="addSelect('del')" />
<div id="pad">
</div>
<input type="submit" name="Submit" value="提 交" onclick="returnnum()"/>
在线等,解决加分! 展开
1个回答
展开全部
<html>
<head>
<title></title>
</head>
<body>
<script>
<!--
var num;//用于标识添加了几个select,在后面的页面用到
num=1;
var objPad;
function addSelect(str)
{
objPad = document.getElementById("pad");
if(str=="del"){
num=1;
//objPad.removeAllChild();
//objPad.innerHTML="";
var selectobj=document.getElementById("pad").getElementsByTagName("select");
var i=selectobj.length;
if(i>0)
{
objPad.removeChild(selectobj[i-1]);
objPad.removeChild(document.getElementById("pad").getElementsByTagName("br")[i-1]);
}
}
else
{
var s= new Array();
s=str.split(",")
var sel = "<select name='ksbj"+num+"'>"
for(var i=0;i<s.length;i++)
{
sel = sel+"<option value='"+s[i]+"'>"+s[i]+"</option>";
}
sel=sel+"</select><br />"
objPad.innerHTML = objPad.innerHTML+sel;
num = num+1;
}
}
function returnnum()
{
num1=num-1
var objPad = document.getElementById("pad");
sel="<input type='hidden' name='cs' value='"+num1+"' />"
objPad.innerHTML = objPad.innerHTML+sel;
}
-->
</script>
<input type="button" value="添加班级" onClick="addSelect('1,2,3')" />
<input type="button" value="删除班级" onClick="addSelect('del')" />
<div id="pad">
</div>
<input type="submit" name="Submit" value="提 交" onclick="returnnum()"/>
</body>
</html>
<head>
<title></title>
</head>
<body>
<script>
<!--
var num;//用于标识添加了几个select,在后面的页面用到
num=1;
var objPad;
function addSelect(str)
{
objPad = document.getElementById("pad");
if(str=="del"){
num=1;
//objPad.removeAllChild();
//objPad.innerHTML="";
var selectobj=document.getElementById("pad").getElementsByTagName("select");
var i=selectobj.length;
if(i>0)
{
objPad.removeChild(selectobj[i-1]);
objPad.removeChild(document.getElementById("pad").getElementsByTagName("br")[i-1]);
}
}
else
{
var s= new Array();
s=str.split(",")
var sel = "<select name='ksbj"+num+"'>"
for(var i=0;i<s.length;i++)
{
sel = sel+"<option value='"+s[i]+"'>"+s[i]+"</option>";
}
sel=sel+"</select><br />"
objPad.innerHTML = objPad.innerHTML+sel;
num = num+1;
}
}
function returnnum()
{
num1=num-1
var objPad = document.getElementById("pad");
sel="<input type='hidden' name='cs' value='"+num1+"' />"
objPad.innerHTML = objPad.innerHTML+sel;
}
-->
</script>
<input type="button" value="添加班级" onClick="addSelect('1,2,3')" />
<input type="button" value="删除班级" onClick="addSelect('del')" />
<div id="pad">
</div>
<input type="submit" name="Submit" value="提 交" onclick="returnnum()"/>
</body>
</html>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询