Well..the title may seem very ambiguous but the problem is kinda new for me.
I have a checkbox which is set n times using a loop (in php).
<input class="shiftt_class" type="checkbox" name="multiSelect[]" value="">
and it is set like this.
<?php foreach($depts['shifts'] as $shfts){?>
<span>
<input class="shiftt_class" type="checkbox" name="multiSelect[]" value="<?php echo $shfts['shift_id'];?>">
<select name="nottime<?php echo $shfts['shift_id']; ?>" class="notification_time_class">
<option value="">Set Time</option>
<option value="11:00" >11:00</option>
<option value="12:00" >12:00</option>
<option value="13:00">13:00</option>
</select>
</span>
<?php }?>
And on a button click, I'm tryna set the checkboxes selected based on a JSON response I'm getting using jQuery.
My JSON response:
[{"shift_id":"2"},{"shift_id":"3"}]
jQuery code:
if shifts is my JSON response, then
if(shifts.length>0)
{
$.each(shifts,function(index,shift)
{
if($(".shiftt_class").val()==shift.shift_id)
{
alert('ddd');
$(".shiftt_class").prop("checked", true);
}
});//end of each function
}
I get the alert working, but the check-boxes are not set. I gotta try the same method to set the select-box as well. Where did I go wrong?
UPDATE: My jQuery function
$.ajax({
url: post_url,
data:{staff_id : staff_id,csrf_test_name : csrf_token},
type: "POST",
dataType: 'json',
beforeSend: function ( xhr ) {
//Add your image loader here
$('#select_loader').show(); // Ajax Loader Show
},
success: function(shifts)
{
$('#select_loader').hide();
$.each(shifts, function (index, shift) {$('.shiftt_class[value="' + shift.shift_id + '"]').prop("checked", true);
});
}
});//end of ajax