思路:设置当前点击节点的checkbox的值为true,其他节点checkbox的值设置为false。
注意:js代码要在文档加载完成后执行才生效,即在$(function(){})中写你的js代码,如果HTML代码本身就在js代码前,$(function(){})则可有可无。
html代码:
<div>
<input type="checkbox" /><span>1</span>
<input type="checkbox" /><span>2</span>
<input type="checkbox" /><span>3</span>
<input type="checkbox" /><span>4</span>
<input type="checkbox" /><span>5</span>
<input type="checkbox" /><span>6</span>
</div>
jQuery代码:
$(function(){
$(":checkbox").click(function(){
//设置当前选中checkbox的状态为checked
$(this).attr("checked",true);
$(this).siblings().attr("checked",false); //设置当前选中的checkbox同级(兄弟级)其他checkbox状态为未选中
});
});