Why are my checkboxes not checking?

Go To StackoverFlow.com

0

I've built a form that pulls in a list of brands from a MYSQL database using JSON. The brands that are pulled in should change the corresponding input brand element in the form. But for some reason I'm not getting this to work.

My code:

JSON

  [{"pkFavorietemerken":"1","merken":"Adidas"},{"pkFavorietemerken":"2","merken":"Boss"},{"pkFavorietemerken":"3","merken":"Burberry"},{"pkFavorietemerken":"4","merken":"Christian Dior"},{"pkFavorietemerken":"5","merken":"D&G"},{"pkFavorietemerken":"6","merken":"Diesel"},{"pkFavorietemerken":"7","merken":"Dolce & Gabanna"},{"pkFavorietemerken":"8","merken":"Emporio Armani"}]

JQUERY:

 $("document").ready(function(){
     $.getJSON("jason2.php", function(data) {
         $.each(data, function(){
             $("[value=this.merken]").attr("checked","checked");
         });
     });

HTML:

<form name="form1" method="post" action="something.php">                        
        <ul>
            <li><input type="checkbox" name="merk[]" value="Adidas"/>Adidas</li>
            <li><input type="checkbox" name="merk[]" value="Airforce"/>Airforce</li>
            <li><input type="checkbox" name="merk[]" value="Armani"/>Armani</li>
            <li><input type="checkbox" name="merk[]" value="Asics"/>Asics</li>
            <li><input type="checkbox" name="merk[]" value="Bikkemberg"/>Bikkemberg</li>
            <li><input type="checkbox" name="merk[]" value="Bjorn Borg"/>Bjorn Borg</li>
            <li><input type="checkbox" name="merk[]" value="BlueBlood"/>BlueBlood</li>
            <li><input type="checkbox" name="merk[]" value="Boss"/>Boss</li>
            <li><input type="checkbox" name="merk[]" value="Brunotti"/>Brunotti</li>
            <li><input type="checkbox" name="merk[]" value="Burberry"/>Burberry</li>
        </ul>
</form>
2012-04-05 18:57
by the_boy_za


2

Try replacing:

$("[value=this.merken]").attr("checked","checked");

with:

$("[value='" + this.merken + "']").attr("checked","checked");
2012-04-05 18:59
by Darin Dimitrov
Did you just solve my 4hours of work in one sec. So the whole problem the whole time was with the string - the_boy_za 2012-04-05 19:14
What about prop()? $el.prop('checked', 1)elclanrs 2012-04-05 19:18
@elclanrs, yes, that's the preferred method over .attr() - Darin Dimitrov 2012-04-05 19:19
Ads