将一组复选框的检查状态复制到另一个字段集

我正在开发项目,我有这样的字段集#a

字段集#a 复选框:已选中值为动态。

我在同一页面上有另一个字段集#b

我想要的是分配/复制复选框:从字段集#a到fieldset #b 检查值在页面加载时。 [两个fieldset具有相同数量的复选框。 ]

 

我怎样才能做到这一点? 谢谢。

您可以使用您提到的选择器执行此操作,然后循环每个值,获取其索引,并根据这些指示更新下一个字段集复选框。

像这样:

 $('fieldset#a input[type="checkbox"]:checked').each(function(){ $('fieldset#b input[type="checkbox"]').eq($(this).index()).prop('checked',true); }); 

例如: http : //jsfiddle.net/niklasvh/hHnLu/

你可以使用这样的东西:

 $( '#b' ).children( 'input:checkbox' ).each( function( i ) { var aChecked = $( '#a' ).children( 'input:checkbox' ).eq( i ).prop( 'checked' ); if ( aChecked ) { $( this ).prop( 'checked', true ); } else { $( this ).removeProp( 'checked' ); } } ); 

我无法让上面的东西工作,因此我尝试了下面的方法,它的工作。

 // lets uncheck all the checkboxes in first fieldset $('#b input[type=checkbox]').removeProp('checked'); cj('#b [type=checkbox]').each(function() { if (cj(this).prop('checked') ) { // here get the index of current element // then modify below code to set prop checked cj('#a [type=checkbox]').prop('checked',true); } }); 

希望这可以帮助!!!