Javascript 访问每个内部的原始对象
我想勾选或取消勾选所有复选框,下面的代码适合我Javascript 访问每个内部的原始对象,javascript,jquery,Javascript,Jquery,我想勾选或取消勾选所有复选框,下面的代码适合我 $( '#chkboxall' ).live( 'change', function() { objs=$(document).find('#chkbox'); objs.each(function(){ $(this).attr( 'checked', $(this).is( ':ch
$( '#chkboxall' ).live( 'change', function() {
objs=$(document).find('#chkbox');
objs.each(function(){
$(this).attr( 'checked', $(this).is( ':checked' ) ? '' : 'checked' );
});
});
但不是使用$(this).is(':checked')?':选中“
我想获取我已选中ie状态$(“#chkboxal”)的原始复选框的状态。我怎样才能在每一个里面做到这一点
Krishnik您可以在不使用here的情况下缩短它(因为它已经可以在集合中的每个元素上运行): 但是,这是无效的,您有一个重复的
chkbox
id
属性,该属性无效。为这些元素指定一个类,如下所示:
<input name="blah" class="chkbox" type="checkbox" />
$('#chkboxall').live('change', function() {
var thing = this;
$('.chkbox').each(function(){
//thing = the outer this, the #chkboxall element
});
});
对于在不同情况下发现此问题的其他人,要获取对内部“outer
this
”的引用,只需设置一个变量,如下所示:
<input name="blah" class="chkbox" type="checkbox" />
$('#chkboxall').live('change', function() {
var thing = this;
$('.chkbox').each(function(){
//thing = the outer this, the #chkboxall element
});
});
非常感谢Nick的工作,我已经更新了html标记,现在我使用类而不是id。再次感谢。