开发者

loop through django template context and jquery object at the same time

开发者 https://www.devze.com 2023-02-18 09:36 出处:网络
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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消