checkedListBox问题
现在在checkedListBox中只有两项,要求如下:当第一项checked,则第二项也自动要checked,如果第二项的checked被取消,则第一项的checked也要自动取消,如果第一项没有checked,则第二项没有限制。
怎么写,谢谢! --------------------编程问答-------------------- js控制
<html xmlns="http://www.w3.org/1999/xhtml">--------------------编程问答--------------------
<head >
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.js"></script>
<script type="text/javascript">
$(function () {
$("input[name='cb']").click(function () {
if ($(this).val() == 0 && !!$(this).attr("checked")) {
$("#cb_1").attr("checked", true)
}
if ($(this).val() == 1 && !$(this).attr("checked")) {
$("#cb_0").attr("checked", false)
}
});
})
</script>
</head>
<body>
<div>
<input type="checkbox" name="cb" value="0" id="cb_0"/><label>测试1</label>
<input type="checkbox" name="cb" value="1" id="cb_1"/><label>测试1</label>
</div>
</body>
</html>
<!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>
function abc(Xelement)
{
var table = document.getElementById(Xelement);
var checkBoxs = table.getElementsByTagName("input");
checkBoxs[0].onclick = function()
{
checkBoxs[1].checked = this.checked;
}
checkBoxs[1].onclick = function()
{
if (checkBoxs[0].checked)
this.checked = true;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:CheckBoxList>
<script> abc("CheckBoxList1")</script>
</form>
</body>
</html>
--------------------编程问答-------------------- 都是web的,我给个桌面的吧!
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBoxList ID="cbl" runat="server">
<asp:ListItem Text="1" Value="a"></asp:ListItem>
<asp:ListItem Text="2" Value="b"></asp:ListItem>
</asp:CheckBoxList>
</form>
<script type="text/javascript">
window.onload = function () {
var cbArr = document.getElementById('<%=cbl.ClientID %>').getElementsByTagName('input');
cbArr[0].onclick = function (cb) {
return function () {
if (this.checked) {
cb.checked = true;
}
}
} (cbArr[1])
cbArr[1].onclick = function (cb) {
return function () {
if (!this.checked) {
cb.checked = false;
}
}
} (cbArr[0])
}
</script>
</body>
</html>
如果就按你说的就两项要求的话,添加selectvaluechanged事件处理函数就行了,如下:
--------------------编程问答-------------------- 在CheckedListBox的ItemCheck事件中写代码:
//注:应将checklistbox1的 ClickOnCllick属性设置为true;
private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (this.checkedListBox1.GetItemChecked(0) == true && this.checkedListBox1.SelectedIndex == 0)
{
this.checkedListBox1.SetItemChecked(1, true);
}
if (this.checkedListBox1.GetItemChecked(1) == false && this.checkedListBox1.SelectedIndex == 1)
{
this.checkedListBox1.SetItemChecked(0, false);
}
}
//如果第一项选中,选中第二项
if(checkListBox.GetItemChecked(0))
{
checkListBox.SetItemChecked(1,true);
}
//如果第二项没有被选中,则取消第一项的选中
if(!checkListBox.GetItemChecked(1))
{
checkListBox.SetItemChecked(0,false);
}
补充:.NET技术 , C#