当打开页面时,我用php的foreach循环动态输出了几个checkbox的选项
<?php foreach ($sectionList as $key => $section): ?>
<?php?>
<li><input type="checkbox" class="cacheSectionSelected" id="checkbox_<?php echo $section->getId() ?>" value="<?php echo $section->getId() ?>" name="cacheSectionSelected[]" ></li>
<?php endforeach; ?>
这个时候没有被勾选上,但我调用其他js函数,比如openSelectOptoin()函数,它会去数据库里判断id = 1是否存在,存在,这时就得将动态添加的checkbox中id与id=1的checkbox的checked=true
部分代码如下:
function openSelectSection(obj) {...
$("#cacheSectionSelected_" + v).each(function(){
$(this).attr("checked",true);
});
}
}
return false;
}
这时其实他是被选上了,但是在网页里确表现为未选上,这是怎么回事呢?我想不明白。难道是因为html是动态的所以不行?
大家帮帮忙,想想是怎么回事呢?
<?php foreach ($sectionList as $key => $section): ?>
<?php?>
<li><input type="checkbox" class="cacheSectionSelected" id="checkbox_<?php echo $section->getId() ?>" value="<?php echo $section->getId() ?>" name="cacheSectionSelected[]" ></li>
<?php endforeach; ?>
这个时候没有被勾选上,但我调用其他js函数,比如openSelectOptoin()函数,它会去数据库里判断id = 1是否存在,存在,这时就得将动态添加的checkbox中id与id=1的checkbox的checked=true
部分代码如下:
function openSelectSection(obj) {...
$("#cacheSectionSelected_" + v).each(function(){
$(this).attr("checked",true);
});
}
}
return false;
}
这时其实他是被选上了,但是在网页里确表现为未选上,这是怎么回事呢?我想不明白。难道是因为html是动态的所以不行?
大家帮帮忙,想想是怎么回事呢?
究竟是什么原因呀?
http://blog.sina.com.cn/s/blog_5a08b1780100duqx.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){
$('#btn').click(function(){
$('input[type="checkbox"]').each(function(i){
if(i%2==1)
{
$(this).attr('checked',!$(this).attr('checked'));
}
})
})
})
</script>
</HEAD> <BODY>
<input type='checkbox' />1
<input type='checkbox' />2
<input type='checkbox' />3
<input type='checkbox' />4
<input type='checkbox' />5
<input type='button' value='click me' id='btn' />
</BODY>
</HTML>
如果 不是的话,最好还是用$("CheckBox的ID").attr("checked","checked");
var v = selects[i];
if (!v) { continue; }
if ($("#cacheSectionSelected_" + v).val() == v) {
$("#cacheSectionSelected_" + v).each(function(){
$(this).attr("checked",true);
});
}
}
我是这样写的
});
=>$("#cacheSectionSelected_" + v).attr("checked",true);