将一组复选框的检查状态复制到另一个字段集 [英] Copy check state of a set of checkboxes to another fieldset

查看:65
本文介绍了将一组复选框的检查状态复制到另一个字段集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在这样的项目中,我有这样的 fieldset #a .

<fieldset id="a">
      <input type="checkbox" name="c1" value="v1" checked="checked" />
      <input type="checkbox" name="c2" value="v2"/>
      <input type="checkbox" name="c3" value="v3"/>
</fieldset>

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

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

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

<fieldset id="b">
      <input type="checkbox" name="cc1" value="vv1"/>
      <input type="checkbox" name="cc2" value="vv2"/>
      <input type="checkbox" name="cc3" value="vv3"/>
</fieldset>

我该如何实现?谢谢.

解决方案

您可以使用您提到的选择器来执行此操作,然后循环使用每个值,获取它们的索引并根据这些标记更新下一个字段集复选框. /p>

赞:

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

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

I'm working on project where I have fieldset #a like this.

<fieldset id="a">
      <input type="checkbox" name="c1" value="v1" checked="checked" />
      <input type="checkbox" name="c2" value="v2"/>
      <input type="checkbox" name="c3" value="v3"/>
</fieldset>

This fieldset #a checkbox : is checked value is Dynamic.

I have another fieldset #b on the same page.

What I want is to Assign/Copy checkbox : is checked value from fieldset #a to fieldset #b Serially on page load. [ Both fieldset have equal number of checkboxes. ]

<fieldset id="b">
      <input type="checkbox" name="cc1" value="vv1"/>
      <input type="checkbox" name="cc2" value="vv2"/>
      <input type="checkbox" name="cc3" value="vv3"/>
</fieldset>

How can I achieve this ? Thanks.

解决方案

You could do it using that selector you mentioned and then looping each of the values, getting their index, and updating the next fieldsets checkboxes according to those indicies.

Like this:

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

example: http://jsfiddle.net/niklasvh/hHnLu/

这篇关于将一组复选框的检查状态复制到另一个字段集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆