I think that the concept is right here but it doesn't seem to work and it's not showing any errors or warnings in the console.
I'm checking checkboxes based on my Django template context. I know that the context is working.
{% f开发者_C百科or neighborhood in neighborhoods %}
$('#neighborhood_id').each(function(){
if ($(this).val()=={{neighborhood}}){
$(this).attr("checked","checked");
}
});
{% endfor %}
thanks
You need to put quotes around {{neighborhood}}
(I am assuming that it's a string). Also, $('#neighborhood_id').each
looks wrong (not that you aren't allowed to do it) because you are not supposed to reuse IDs, having an each
there seems like you have more than one. Lastly, why can't you just check them in your Django template?
Sorry, I posted too early. i had duplicate #neighborhood_id in my markup that was screwing it up.
精彩评论