0

I have this code to get back a json string:

$.getJSON("<?php echo BASE_URL; ?>/editTask.php?ID="+tid,function(result){
        $.each(result.staff, function() { 
            $("#checkbox[value=" + this + "]").prop('checked');
        });
});

The json looks like this

{"staff":[13,17,15]}

I have a PHP snippet like this

<div id="checkbox" class="checkbox" style="max-height:150px;overflow:auto;">
<?php
for ($i = 0; $i < count($user_list); ++$i) {
  echo '<label>'
      .'<input type="checkbox" name="tasksUser[]" 
           value="'.$user_list[$i]['uid'].'">'
      .$user_list[$i]['surname'].', '.$user_list[$i]['forename']
      .'</label><br />';
}
?>
</div>

I want that every checkbox that has the value that is in result.staff be checked. But I think I have an error in my each part. But in the Firefox console is no error showing.

Could you help me?

4
  • what actually is your error then? does it not work, does it check some of them? Commented Dec 23, 2016 at 15:13
  • No item is checked. Bute I know that they matched. Commented Dec 23, 2016 at 15:24
  • Yea because you have few issue in your code. You are trying to use id but there is no id. Commented Dec 23, 2016 at 15:28
  • formatted to more readable code Commented Dec 23, 2016 at 22:55

2 Answers 2

1

Yes there is an issue in your $.each function.

 $.each(result.staff, 
     function() { 
         $("#checkbox[value=" + this + "]").prop('checked');
            ^
            Here is the issue.
     });
 });

As you don't have any id or class associated with element you can access it using element name/+type

You must replace your code with

$("input[type='checkbox'][value=" + this + "]").prop("checked", true);

Here I am accessing all checkboxes with values available and setting checked as true.

Sign up to request clarification or add additional context in comments.

Comments

0

You're using this selector to look for checkboxes:

$("#checkbox[value...]")

However, the # symbol in a selector looks for an element with that ID.

The checkboxes in your PHP loop do not have a specified ID or class, so you could select them with:

#(":checkbox[value...]")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.