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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵