当我单击按钮(复选框)时,列表框中的所有项目都会消失。如何防止这种情况发生?我的删除功能无法正常工作。是因为我的 javascript 吗?我的代码如下:
.aspx 文件
<tr>
<td width="150px">
<asp:TextBox ID="TextBox1" runat="server" onkeypress="displayKeyCode_demo(event)"></asp:TextBox>
</td>
<td width="200px">
<asp:ListBox ID="LstScan" runat="server" Height="200px" Width="150px" >
</asp:ListBox>
<asp:HiddenField runat="server" ID="hdnScan"/>
</td>
<td width="100px">
<asp:Button ID="Button2" runat="server" Text="Remove" OnClientClick="removeItem()" />
</td></tr></table>
javascript:
<script type="text/javascript">
function displayKeyCode_demo(e) {
var code;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
if (code == 13) {
var ListBox = document.getElementById("<%=LstScan.ClientID%>");
var TextBox = document.getElementById("<%=TextBox1.ClientID%>");
var hdnScan = document.getElementById("<%=hdnScan.ClientID%>");
var myOption = new Option();
myOption.text = document.getElementById("<%=TextBox1.ClientID%>").value.trim(); //Textbox's value
myOption.value = document.getElementById("<%=TextBox1.ClientID%>").value.trim(); //Textbox's value
hdnScan.value = hdnScan.value + "," + TextBox.value;
ListBox.appendChild(myOption);
document.getElementById("<%=TextBox1.ClientID%>").value = '';
// e.preventDefault();
}
}
function removeItem() {
// var i;
// var selectbox = document.getElementById("<%=LstScan.ClientID%>");
// for (i = selectbox.options.length - 1; i >= 0; i--) {
// if (selectbox.options[i].selected)
// selectbox.remove(i);
// }
document.getElementById("<%=LstScan.ClientID%>").options[document.getElementById("<%=LstScan.ClientID%>").selectedIndex] = null;
}