Jquery function not defined with onclick attribute set inside django forms (forms.py)

好久不见. 提交于 2020-01-07 04:40:11

问题


I have a django form with onclick attribute set to a jQuery function located inside a .JS file and I want that function to be called whenever the onclick event is triggered . When I click on the form , it says my function is not defined . Maybe the answer is easy and obvious but I'm kind of newbie in both jQuery and Django .I have googled it around but still can't work this out .

Any help would be great .

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>

来源:https://stackoverflow.com/questions/45264701/jquery-function-not-defined-with-onclick-attribute-set-inside-django-forms-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!