Check Flags Loaded From JQuery Download

I am loading a set of checkboxes into a div using jQuery and the load function. My code

$("#cb-list").load('www.someurl.com');

where www.someurl.com returns html for a set of flags, for example.

<input type="checkbox" value="sddfdsf" name="cb[]" id="a1" />
<input type="checkbox" value="sddfdsf" name="cb[]" id="b2" />

I want to view the checkboxes that were returned, and then check some of them based on the parameters (I want to do this on a page that loads the checkboxes, not the page being loaded).

How do I do this using jQuery? So, for example, I can have 3 flags with identifiers a1, b2, c3, and I would like to check b2 and c3. I usually did

$("#b2").attr("checked") = 'checked';
$("#c3").attr("checked") = 'checked';

But this does not work, and I assume that this is because the checkboxes are loaded from an external link.

thank

+3
source share
2

, AJAX HTML-, jQuery:

//put html into a string
var myCheckboxes = $("#cb-list").load('www.someurl.com');
/*... possibly perform some validations here ... */
var $myCheckboxes = $(myCheckboxes);

:

$myCheckboxes.filter("#b2").attr('checked', true);
$myCheckboxes.filter("#c3").attr('checked', true);

, .each():

$myCheckboxes.each(function(){
    $(this).attr('checked', true);
});

, -. div id ='MyDiv'

$myCheckboxes.appendTo("#MyDiv");
+4

$("#b2").attr('checked', true);

+3

Source: https://habr.com/ru/post/1728901/


All Articles