用class检查每个div旁边的输入

时间:2015-06-02 11:08:32

标签: jquery checkbox

当我加载页面时,我试图检查所有输入。

我的HTML是:

    <td class="even bundlecheckbox">
    <input onclick="bundle.changeSelection(this)" class="checkbox bundle-option-1 validate-one-required-by-name" id="bundle-option-1-2" type="checkbox" name="bundle_option[1][]" value="2"></td>

它可能重复10次或4次。

我尝试使用jQuery这样做:

jQuery(document).ready(function() {
    jQuery('.bundlecheckbox').next('input').prop("checked",true);
});

但我想我错过了什么。

2 个答案:

答案 0 :(得分:3)

Checkbox是一个孩子,所以请使用Descendant Selector ("ancestor descendant")

  

选择作为给定祖先后代的所有元素。

使用

jQuery('.bundlecheckbox input:checkbox').prop("checked",true);

,您还可以使用.find().children()

jQuery('.bundlecheckbox').find('input:checkbox').prop("checked",true);

答案 1 :(得分:2)

input是td class .bundlecheckbox的孩子,它不是sibiling或下一个元素

jQuery('.bundlecheckbox').find("input[type='checkbox']").prop("checked",true);

更改值并禁用

jQuery('.bundlecheckbox').find("input[type='checkbox']").prop("value", "3");
jQuery('.bundlecheckbox').find("input[type='checkbox']").prop("disabled", true)