可以不可以在

qq2013 2008-02-26 05:48:09

可以不可以在 <script language="javascript" type="text/javascript">里面给DropDownList添加项

可以不可以在页面中运行javascript的事件清空DropDownList原来的项.再给DropDownList添加一些项,
DropDownList可以是服务控件也可以不是,最好是!
如果可以,就问
javascript的代码怎么写呢!
...全文
114 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿非 2008-02-28
  • 打赏
  • 举报
回复
?
fullhouse 2008-02-26
  • 打赏
  • 举报
回复
楼上2位的代码想必就已经可以了。
因为我最近也作了这么个东西。
copy个清空的吧。
...
var objLstADR1 = document.getElementById("ctl00$MCt1$lstADR1")
var lstLen = objLstADR1.options.length;
for(var i=1; i<=lstLen; i++){
objLstADR1.remove(lstLen-i);
}
...
都是google出来的。
另外,要注意的是,
js侧变更DropDownList的内容只在Client侧有效。
阿非 2008-02-26
  • 打赏
  • 举报
回复

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownList.aspx.cs" Inherits="DropDownList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
function add()
{
var text=document.getElementById("Text1").value;
var value=document.getElementById("Text2").value;
document.getElementById("ddl").options[document.getElementById("ddl").length]= new Option(text,value);
}

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddl" runat="server" Width="127px">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
<asp:ListItem>D</asp:ListItem>
<asp:ListItem>E</asp:ListItem>
</asp:DropDownList> <br />
text: <input id="Text1" style="width: 56px" type="text" />
value: <input id="Text2" style="width: 55px" type="text" /><br />
<input id="Button2" type="button" value="添加" onclick="add();"/>
<input id="Button1" type="button" value="清空" onclick="javascript:document.getElementById('ddl').options.length=0;" /> 
</div>
</form>
</body>
</html>

sunzhong2003 2008-02-26
  • 打赏
  • 举报
回复
function add_option( name , texts , value )
{
var selObj = getElement( name );
selObj.options[selObj.length]=new Option( texts , value );
}
qq2013 2008-02-26
  • 打赏
  • 举报
回复
好的,等你的代码啦!
阿非 2008-02-26
  • 打赏
  • 举报
回复
可以,等下帮你写个demo

62,256

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧