看到代码即可明白小弟意思。点击按钮后发现选项任然存在,(查看html代码也在)不知道其中什么机制。要实现此功能如何做。谢谢!<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %><!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">
<script type ="text/javascript" >
function del(){
var sel=document.getElementById("sel");
var len=sel.options.length;
alert(len);
for(var i=len-1;i>=0;i--){
sel.remove(i);
}
}
</script>
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="sel" runat="server" Height="104px" Width="176px">
<asp:ListItem Value="a">1</asp:ListItem>
<asp:ListItem Value="b">2</asp:ListItem>
</asp:ListBox>
<asp:Button ID="btn" runat="server" Text="Button" OnClientClick ="del();"/>
</div>
</form>
</body>
</html>
<head runat="server">
<script type ="text/javascript" >
function del(){
var sel=document.getElementById("sel");
var len=sel.options.length;
alert(len);
for(var i=len-1;i>=0;i--){
sel.remove(i);
}
}
</script>
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="sel" runat="server" Height="104px" Width="176px">
<asp:ListItem Value="a">1</asp:ListItem>
<asp:ListItem Value="b">2</asp:ListItem>
</asp:ListBox>
<asp:Button ID="btn" runat="server" Text="Button" OnClientClick ="del();"/>
</div>
</form>
</body>
</html>
谢谢
function SelectClear(obj)
{
var tLength=obj.options.length;
for (var i=0;i<tLength;i++)
{
obj.options.remove(0);
}
}给你一段代码
obj.options.remove(0);
需要有返回值的,默认是return true
因为你应用的是服务器控件。
及时你删除成功那么也会重新刷新下页面(还原了)
如果return false则不会进入服务器段
{
var tLength=obj.options.length;
for (var i=0;i<tLength;i++)
{
obj.options.remove(0);
}
}
结果 无法实际操作服务器控件listbox
所以发问。因为比较急 后来是这样解决的:子画面关闭 调用父画面JavaScript:这里随便写个方法 触发点击服务器控件 比如button 然后再cs代码中操作添加或删除listbox项目。
具体是参照了别人的做法:http://zhidao.baidu.com/question/88603512.html?fr=ala0谢谢大家。有碰到类似问题的初学者也可参考。