gpt4 book ai didi

jquery - 如何使用 JQuery 根据另一组复选框检查一组复选框

转载 作者:行者123 更新时间:2023-12-01 08:13:38 24 4
gpt4 key购买 nike

我的页面上有两个单独的表单,并且它们都有一组复选框。

<body>
<form name="form1" id="form1" ...>
<input type="checkbox" name="check1" value="Person" />
<input type="checkbox" name="check1" value="Dog" />
<input type="checkbox" name="check1" value="Cat" />
</form>
<form name="form2" id="form2" ...>
<input type="checkbox" name="check1" value="Person" />
<input type="checkbox" name="check1" value="Dog" />
<input type="checkbox" name="check1" value="Cat" />
</form>
</body>

我在这里需要的是,一旦用户选中/取消选中 form2 中的任何复选框,我想对 form1 中的相应复选框执行相同的操作。如果用户在 form2 中选择“dog”和“cat”,则自动对 form1 进行相同的检查。我已经尝试了一些方法,但我无法找到一种根据值字段设置选中属性的方法。

我能做的最好的事情就是为所有复选框提供唯一的 ID,并根据 ID 选择/取消选择。但我正在尝试找出是否有更好的选择器方式。

另外,如果有人能建议我更好的方法来完成我在这里尝试的事情,我会很高兴。

--我还没有尝试过,但这就是我打算做的---

   <form name="form1" id="form1" ...>
<input type="checkbox" name="check1[]" id="form1_Person" value="Person" />
<input type="checkbox" name="check1[]" id="form1_Dog" value="Dog" />
<input type="checkbox" name="check1[]" id="form1_Cat" value="Cat" />
</form>
<form name="form2" id="form2" ...>
<input type="checkbox" name="check1[]" id="form2_Person" value="Person" />
<input type="checkbox" name="check1[]" id="form2_Person" value="Dog" />
<input type="checkbox" name="check1[]" id="form2_Person" value="Cat" />
</form>

单击 form2 复选框后,获取值,并在 form1 中查找复选框。如果选择了狗,请检查 $('#form1_'[dog]).checked('checked',checked)

最佳答案

你可以这样做

$('input[type=checkbox]').change(function(){ // <-- bind to all checkboxes
var $this = $(this);
var x = $this.index(); // store index
var $otherForm = $('form').not($this.parent()); // need this to synch other form
$otherForm.find('input').eq(x).prop('checked',this.checked); // change other forms checkbox accordingly
});​

http://jsfiddle.net/wirey00/ewfrV/

关于jquery - 如何使用 JQuery 根据另一组复选框检查一组复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12062267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com