JS判断复选框,有四个复选框,条件如下
选第二个选择框的时候第一个,第二个被选中,选第三个复选框的时候123被选中,选第四个复选框的的时候1234被选中,然后点哪个复选框哪个复选框要能被取消选中,求大神指点,说实话我已经达到这个效果可尼玛在点的时候取消不了……
选第二个选择框的时候第一个,第二个被选中,选第三个复选框的时候123被选中,选第四个复选框的的时候1234被选中,然后点哪个复选框哪个复选框要能被取消选中,求大神指点,说实话我已经达到这个效果可尼玛在点的时候取消不了……
<script type="text/javascript">
function selectMe(o,index){
for(i=0;i<o.length;i++)
{
if(i<index)
{
if(o[index].checked){o[i].checked=true;}
}
}
}
function initMe(){
var o=document.getElementsByName("me");
for(i=0;i<o.length;i++)
{
(function(index){
o[index].onclick=function(){
selectMe(o,index);
}
})(i)
}
}
window.onload=function(){
initMe();
}
</script>
<input type="checkbox" name="me" />
<input type="checkbox" name="me" />
<input type="checkbox" name="me" />
<input type="checkbox" name="me" />
<script type="text/javascript">
function initMe(){
var o=document.getElementsByName("me");
for(i=0;i<o.length;i++)
{
(function(index){
o[index].onclick=function(){
for(j=0;j<index;j++)
{
if(o[index].checked){o[j].checked=true;}
}
}
})(i)
}
}
window.onload=function(){
initMe();
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script language="javascript">
$(document).ready(function () {
$(":checkbox").click(function () {
if (this.checked) {
var cbID = $(this).attr("id");
for (var i = 1; i <= cbID; i++) {
$("#" + i).attr("checked","true");
}
}
}); });
</script>
</head>
<body>
<input type="checkbox" id="1" />第一个checkbox<br />
<input type="checkbox" id="2" />第二个checkbox<br />
<input type="checkbox" id="3" />第三个checkbox<br />
<input type="checkbox" id="4" />第四个checkbox<br />
</body>
</html>
<script language="javascript">
$(function(){
$(':checkbox').click(function(){
//获取当前点击复选框前面的同辈复选框元素的个数
var count = $(this).prevAll(':checkbox').size();
if($(this).get(0).checked){
for(var i = 0;i < count;i++){
$(':checkbox').get(i).checked = true;
}
}
});
})
</script>
<input type="checkbox" />one
<input type="checkbox" />two
<input type="checkbox" />three
<input type="checkbox" />four