实现功能:
在页面里有个TextBox 和一个CheckBoxList.
当TextBox用户扫描进一个字符串,会与CheckBoxList的ListItem的值比较,如果相等,就把相应的ListItem勾选上。
代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www.
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" language="javascript">
function check() {
var oCBList = document.getElementById("<%=cblTest.ClientID %>");
var tds = oCBList.getElementsByTagName("td");
var inputs = oCBList.getElementsByTagName("input");
var oTextBox = document.getElementById("tbTest");
for (var i = 0; i < tds.length; i++) {
if (oTextBox.value == tds[i].childNodes[1].innerText) {
inputs[i].checked = true;
}
}
}
function checkAll() {
var oCBList = document.getElementById("<%=cblTest.ClientID %>");
var inputs = oCBList.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
inputs[i].checked = true;
}
}
function unCheckAll() {
var oCBList = document.getElementById("<%=cblTest.ClientID %>");
var inputs = oCBList.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
inputs[i].checked = false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="tbTest" runat="server"></asp:TextBox>
<input id="Button3" type="button" value="Check" onclick="check();"/><br />
<br />
<input id="Button1" type="button" value="Html_CheckAll" onclick="checkAll();" />
<asp:Button ID="Button2" runat="server" Text="Aspx_UnCheckAll" OnClientClick="unCheckAll();" />
<br />
</div>
<asp:CheckBoxList ID="cblTest" runat="server" Width="158px" BackColor="#CCFFFF">
<asp:ListItem>001</asp:ListItem>
<asp:ListItem>002</asp:ListItem>
<asp:ListItem>003</asp:ListItem>
<asp:ListItem>004</asp:ListItem>
<asp:ListItem>005</asp:ListItem>
</asp:CheckBoxList>
</form>
</body>
</html>