问题描述:

I have these checkboxes:

<input type="checkbox" id-mod="1" name="read[]" />

<input type="checkbox" id-mod="4" parent="1" name="read[]" />

<input type="checkbox" id-mod="10" parent="4" name="read[]" />

<input type="checkbox" id-mod="60" parent="10" name="read[]" />

when checking an entry containing the attribute "parent" should automatically checked input containing the attribute "id-mod" of the same value.

Use the following function:

$("input:checkbox[parent]").change(function() {

if ($(this).is(':checked')) {

var parent = $(this).attr('parent');

$("input:checkbox[id-mod='"+parent+"']").prop("checked", true);

}

});

For example, when check input with the attribute parent="10", it also checked the input with the attribute id-mod="10", but I need this it in turn get the same event with the attribute parent="4" and successively... How do I get this?

网友答案:

Create an function which takes an id to check. The function checks if it has an parent. If an parent is found it will call the same function again with that id.

This can however easily become an infinite loop. And your browser will not respond.

网友答案:

You can try matching their attribute as

// run the selector and then..
var parent = $(this).attr('parent');//get parent value
$(this).parent().find('input[id-mod=' + parent + ']').prop('checked', true);

Then find the input from the parent element, (or if you have a class or id for the form you can use that too) and after finding update its property to checked.

http://jsfiddle.net/afzaal_ahmad_zeeshan/YFXKL/ here is a fiddle for this! :)

相关阅读:
Top