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>
Try replacing:
$("[value=this.merken]").attr("checked","checked");
with:
$("[value='" + this.merken + "']").attr("checked","checked");
prop()
? $el.prop('checked', 1)
elclanrs 2012-04-05 19:18