Javascript Jquery函数未在django表单(forms.py)中使用onclick属性集定义

Javascript Jquery函数未在django表单(forms.py)中使用onclick属性集定义,javascript,jquery,html,django,django-forms,Javascript,Jquery,Html,Django,Django Forms,我有一个django表单,其onclick属性设置为位于.JS文件中的jQuery函数,我希望在触发onclick事件时调用该函数。当我点击表单时,它说我的函数没有定义。也许答案简单明了,但我对jQuery和Django都是新手。我已经在谷歌上搜索过了,但仍然无法找到答案 任何帮助都会很好 forms.py class UserProfileForm(forms.ModelForm): class Meta: model = Profile fields

我有一个django表单,其onclick属性设置为位于.JS文件中的jQuery函数,我希望在触发onclick事件时调用该函数。当我点击表单时,它说我的函数没有定义。也许答案简单明了,但我对jQuery和Django都是新手。我已经在谷歌上搜索过了,但仍然无法找到答案

任何帮助都会很好

forms.py

class UserProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = (
           'nationality'
) 
        widgets = {
            'nationality': forms.TextInput(attrs={'placeholder': 'Your Country Name ...', 'onclick': 'autocompleteCountry()' }),
}
index.js

$(document).ready(function(){
   function loadAutocomplete(id, country){
    $(function(){
      $(id).autocomplete({
        source: country
      });
    });
  }

  function autocompleteCountry(country){
    loadAutocomplete('#id_nationality', country);
  }
};
index.html

<form method="post">
                        {% csrf_token %}
                            <div class="fieldset">
                                {% for field in f %}
                                        {{ field|as_crispy_field }}
                                {% endfor %}
                            </div>
</form>

{%csrf_令牌%}
{f%中字段的%s}
{{field | as_crispy_field}
{%endfor%}

您已经在
$(document.ready()
中定义了您的
自动完成国家/地区
函数。将您的
autocompleteCountry
定义移动到全局范围内,即
$(文档)之外。ready()
@ohannes,谢谢。错误消失了,但我想我可以实现我想要的。请你把你的邮件给我,以便我们讨论。无法聊天,声誉不足。您已在
$(document.ready()
中定义了您的
autocompleteCountry
函数。将您的
autocompleteCountry
定义移动到全局范围内,即
$(文档)之外。ready()
@ohannes,谢谢。错误消失了,但我想我可以实现我想要的。请你把你的邮件给我,以便我们讨论。不能聊天,没有足够的声誉。