作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的页面上有两个单独的表单,并且它们都有一组复选框。
<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
});
关于jquery - 如何使用 JQuery 根据另一组复选框检查一组复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12062267/
我是一名优秀的程序员,十分优秀!