怎么用JavaScript实现ListBox的添加移除项功能

SunFee 2005-01-09 01:46:36
protected System.Web.UI.WebControls.ListBox ListBox1;
ListBox1.Items.Add("Test");
ListBox1.Items.Remove(ListBox1.SelectedItem);

用ASP.net的话等于每次都是刷新页面了...

希望用脚本语言实现
但是一些网站的源代码看不懂,达人给讲讲吧,3x
需要做一个搜索器:P
...全文
1017 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
SunFee 2005-01-09
  • 打赏
  • 举报
回复
非常感谢,刚才已经发现少了括号了,加上也没用,又仔细一看原来拼写错了- -~

问题解决,非常感谢
wj2929 2005-01-09
  • 打赏
  • 举报
回复
this.Button1.Attributes.Add("onclick","javascript:buttonclick();return false;");

忘了些"()"了
SunFee 2005-01-09
  • 打赏
  • 举报
回复
this.Button1.Attributes.Add("onclick","javascript:buttonclick;return false;");

这句是写在Page_Load吗,怎么不起作用呢,点击Button1还是提交服务器刷新页面了
wj2929 2005-01-09
  • 打赏
  • 举报
回复
比如
添加数据:
this.Button1.Attributes.Add("onclick","javascript:buttonclick;");

可以在Button1触发服务器端事件前先触发脚本事件

如果写成下面这样
this.Button1.Attributes.Add("onclick","javascript:buttonclick;return false;");
只触发脚本事件不提交服务器

welshem 2005-01-09
  • 打赏
  • 举报
回复
Button1的服务器代码去实现你的其他操作
welshem 2005-01-09
  • 打赏
  • 举报
回复
向asp:button的Button注册onClick事件,如
Button1.Attributes.Add("onclick","deldata();");
SunFee 2005-01-09
  • 打赏
  • 举报
回复
如果添加数据的按钮我使用的是asp:button呢,因为除了添加到Listbox还需要执行别的一些操作
或者有什么别的方法能代替
wj2929 2005-01-09
  • 打赏
  • 举报
回复
参考:

<script>
function buttonclick()
{
var objtext=document.getElementById("Text1");
var objlist=document.getElementById("Select1");
if(objtext.value!="")
{
node = document.createElement("<option value='"+objtext.value+"'>");
text = document.createTextNode(objtext.value);
node.appendChild(text);
objlist.appendChild(node);
}
}
function deldata()
{
var objtext=document.getElementById("Text1");
var objlist=document.getElementById("Select1");
for(i=objlist.length-1;i>=0;i--)
{
if(objlist.options[i].selected ==true)
{
objlist.remove(i);
}
}
}
</script>
<INPUT id="Text1" type="text" name="Text1" DESIGNTIMEDRAGDROP="24">  <INPUT id="Button1" type="button" value="添加数据" name="Button1" onclick="javascript:buttonclick();"><SELECT id="Select1" size="11" name="Select1" style="WIDTH:100px">
</SELECT>
<INPUT id="Button2" type="button" value="删除选中数据" name="Button2" onclick=javascript:deldata();>
SunFee 2005-01-09
  • 打赏
  • 举报
回复
谢谢 wj2929
我是想首先显示一个空的ListBox

然后点击一个按钮添加一个条目进ListBox中,移除也是通过按钮事件来完成

不知道在C#中结合按钮应该怎么写
wj2929 2005-01-09
  • 打赏
  • 举报
回复
添加 项
参考:
<SELECT id="Select1" size="2" name="Select1">
<OPTION></OPTION>
</SELECT>
<script>
var s="1<br/>2<br/>3<br/>4<br/>";
var a = s.split("<br/>");
var obj=document.getElementById("Select1");

for(i=0;i<a.length-1;i++)
{
if(a[i]!="")
{
node = document.createElement("<option value='"+a[i]+"'>");
text = document.createTextNode(a[i]);
node.appendChild(text);
obj.appendChild(node);
}

}
</script>

主要借助下面四句

node = document.createElement("<option value='1'>");
text = document.createTextNode("1");
node.appendChild(text);
obj.appendChild(node);


移除 项
var obj=document.getElementById("Select1");
obj.remove(0); //移除首项
for(i=0;i<obj.length;i++) //移除所有项
{
obj.remove(0); //移除首项
}

62,074

社区成员

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

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

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

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