weixin_33709609 2015-06-11 08:50 采纳率: 0%
浏览 29

Django Javascript集成

I am trying to create a dynamic list(type radio) when one selects an option. I am able to get the values of the list but I am unable to view it on the page.

This is the code: javascript:

$(document).ready(function() {
    $("select[name=city]").change(function() {
        if ($(this).val() == '') {
            $("select[name=term]").html("<option>Select a frequency</option>");
            $("select[name=term]").attr('disabled', true);
        }
        else {
            var url = "/locality/city/" + Number($(this).val());
            console.log(url)

            $.getJSON(url, function(frequency_list) {
                var li = '<ul id="id_term"><li><label><input id="id_term_0" type="radio" value="-1" name="term"></input>None</label></li>'

                for (var i = 0; i < frequency_list.length; i++) {
                    // console.log(frequency_list[i][0],frequency_list[i][0]);
                    var id = "id_term_" + String(i)
                    li += '<li><label><input id='+String(id)+ ' type="radio" value='+frequency_list[i][0]+ ' name="term"></input>'+frequency_list[i][1]+'</label></li>'
                    console.log(li);
                }

                li +='</ul>'
                $("select[name=term]").html(li);
                // $("select[name=term] li:first").attr('selected', 'selected');
                $("select[name=term]").attr('disabled', false);
            });
        }
    });

    $("select[name=term]").change(function(vent) {
        if ($(this).val() == -1) {
            return;
        }
    });
});

forms:

class CityForm(forms.Form):
    city = forms.ModelChoiceField(queryset=City.objects.all().filter(tagged=True))

class FrequencyForm(CityForm):
    """
    Generates a form consisting of a list of trigrams, bigrams and unigrams in ratio 8:8:2
    """

    def __init__(self, *args, **kwargs):
        super(FrequencyForm, self).__init__(*args, **kwargs)
        self.frequency_list = [('-1','None')]
        self.fields['term'] = forms.ChoiceField(choices=self.frequency_list, required=True, widget=forms.RadioSelect())
  • 写回答

1条回答 默认 最新

  • weixin_33688840 2015-06-11 13:01
    关注

    If your list gets out ok from your getJSON call, the problem is probably when you're trying to inject content that is not <option> in a <select> element. See <select> specs: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select It says:

    Permitted content Zero or more option or optgroup elements.

    So this line will most likely not give any result:

    $("select[name=term]").html(li);
    

    So either you add afterwards or you create a new non select element to contain you list.

    评论

报告相同问题?

悬赏问题

  • ¥15 三菱变频器模块怎么检测触发
  • ¥30 求一下解题思路,完全不懂。
  • ¥30 关于#硬件工程#的问题:求一下解题思路
  • ¥15 运筹学对偶单纯行法构造扩充问题
  • ¥20 XP系统的老电脑一开机就提示找不到rundll.exe,付费求解
  • ¥15 milvus查询出来的score怎么转换成0-1之间的相似性
  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗
  • ¥15 Sharepoint JS开发 付费技术指导