在页面加载时调用JQuery函数

时间:2016-05-18 12:15:54

标签: jquery django

我刚刚开始使用JQuery,这开始让我疯狂 代码适用于Django项目。

模板

{% extends "core/base.html" %}
{% load url from future %}


{% block title %}Register Company{% endblock %}

{% block content %}
    <div class="container">

        <script>
            $(document).ready(function () {
                $("#country_subdivision").hide();
                $("#address").hide();
                $("#postal_code").hide();
                $("#city").hide();

                var states;
                $("#id_company_country").change(function () {
                    var country = $("#id_company_country").val();
                    states = $.get("{% url 'get_country_subdivisions' 1 %}", function (data, status) {

                    });
                    alert(states);
                    $("#id_company_country_subdivision").empty();
                    $("#country_subdivision").show();

                    $(JSON.parse(states.country_subdivisions)).map(function () {
                        return $('<option>').val(this.value).text(this.label);
                    }).appendTo('#id_company_country_subdivision');
                });

                $("#id_company_country_subdivision").change(function () {
                    $("#address").show();
                    $("#postal_code").show();
                    $("#city").show();
                });
            })
        </script>
        <h1>Registration form</h1>
        <form class="ci-form" action="" method="post">
            {% csrf_token %}
            {% if error_message %}
                <div class="alert alert-danger">{{ error_message }}</div>
            {% endif %}
            <h2>Company information</h2>
            <div><span><label
                    for="{{ form.company_name.id_for_label }}">{{ form.company_name.label }} </label>{{ form.company_name }}{{ form.company_name.error }}</span>
                <span class="ci-inline-error">{{ form.company_name.errors.as_text }}</span></div>
            <br/>
            <div><span><label for="{{ form.company_phone.id_for_label }}">{{ form.company_phone.label }} </label>{{ form.company_phone }}</span>
                <span class="ci-inline-error">{{ form.company_phone.errors.as_text }}</span></div>
            <div><span><label for="{{ form.company_fax.id_for_label }}">{{ form.company_fax.label }} </label>{{ form.company_fax }}</span>
                <span class="ci-inline-error">{{ form.company_fax.errors.as_text }}</span></div>
            <br/>
            <div><span><label for="{{ form.company_email.id_for_label }}">{{ form.company_email.label }} </label>{{ form.company_email }}</span>
                <span class="ci-inline-error">{{ form.company_email.errors.as_text }}</span></div>
            <div><span><label for="{{ form.company_website.id_for_label }}">{{ form.company_website.label }} </label>{{ form.company_website }}</span>
                <span class="ci-inline-error">{{ form.company_website.errors.as_text }}</span></div>
            <br/>
            <div id="country"><span><label
                    for="{{ form.company_country.id_for_label }}">{{ form.company_country.label }} </label>{{ form.company_country }}</span>
                <span class="ci-inline-error">{{ form.company_country.errors.as_text }}</span></div>
            <div id="country_subdivision"><span><label
                    for="{{ form.company_country_subdivision.id_for_label }}">{{ form.company_country_subdivision.label }} </label>{{ form.company_country_subdivision }}</span>
                <span class="ci-inline-error">{{ form.company_country_subdivision.errors.as_text }}</span></div>
            <div id="address"><span><label
                    for="{{ form.company_address.id_for_label }}">{{ form.company_address.label }} </label>{{ form.company_address }}</span>
                <span class="ci-inline-error">{{ form.company_address.errors.as_text }}</span></div>
            <div id="postal_code"><span><label
                    for="{{ form.company_postal_code.id_for_label }}">{{ form.company_postal_code.label }} </label>{{ form.company_postal_code }}</span>
                <span class="ci-inline-error">{{ form.company_postal_code.errors.as_text }}</span></div>
            <div id="city"><span><label
                    for="{{ form.company_city.id_for_label }}">{{ form.company_city.label }} </label>{{ form.company_city }}</span>
                <span class="ci-inline-error">{{ form.company_city.errors.as_text }}</span></div>

            <h2 id="user_informatino">User information</h2>
            <div id="username"><span><label
                    for="{{ form.user_username.id_for_label }}">{{ form.user_username.label }} </label>{{ form.user_username }}</span>
                <span class="ci-inline-error">{{ form.user_username.errors.as_text }}</span></div>
            <div id="pwd"><span><label
                    for="{{ form.user_password.id_for_label }}">{{ form.user_password.label }} </label>{{ form.user_password }}</span>
                <span class="ci-inline-error">{{ form.user_password.errors.as_text }}</span></div>
            <div id="pwd2"><label for="{{ form.user_password_2.id_for_label }}">{{ form.user_password_2.label }} </label>{{ form.user_password_2 }}
                <span class="ci-inline-error">{{ form.user_password_2.errors.as_text }}</span></div>
            <br/>
            <div id="first_name"><span><label
                    for="{{ form.user_first_name.id_for_label }}">{{ form.user_first_name.label }} </label>{{ form.user_first_name }}</span>
                <span class="ci-inline-error">{{ form.user_first_name.errors.as_text }}</span></div>
            <div id="initials"><span><label
                    for="{{ form.user_initials.id_for_label }}">{{ form.user_initials.label }} </label>{{ form.user_initials }}</span>
                <span class="ci-inline-error">{{ form.user_initials.errors.as_text }}</span></div>
            <div id="last_name"><span><label
                    for="{{ form.user_last_name.id_for_label }}">{{ form.user_last_name.label }} </label>{{ form.user_last_name }}</span>
                <span class="ci-inline-error">{{ form.user_last_name.errors.as_text }}</span></div>
            <div id="gender"><span><label for="{{ form.user_gender.id_for_label }}">{{ form.user_gender.label }} </label>{{ form.user_gender }}</span>
                <span class="ci-inline-error">{{ form.user_gender.errors.as_text }}</span></div>
            <br/>
            <div id="user_phone"><span><label
                    for="{{ form.user_phone.id_for_label }}">{{ form.user_phone.label }} </label>{{ form.user_phone }}</span>
                <span class="ci-inline-error">{{ form.user_phone.errors.as_text }}</span></div>
            <div id="user_email"><span><label
                    for="{{ form.user_email.id_for_label }}">{{ form.user_email.label }} </label>{{ form.user_email }}</span>
                <span class="ci-inline-error">{{ form.user_email.errors.as_text }}</span></div>

            <input type="submit" class="ci-button margin-bottom-20" value="Register >>">
        </form>
    </div>
{% endblock %}

{% block footer %}
    {{ block.super }}
{% endblock %}

URL

url(r'^get_country_subdivisions/(?P<country_id>\d*)/$', get_country_subdivisions_for_country, name="get_country_subdivisions")

获取细分的方法

def get_country_subdivisions_for_country(request, country_id):
    if country_id:
        country_subdivisions = CountrySubdivision.objects.filter(country_id=country_id)
        if len(country_subdivisions):
            country_subdivisions = [{"value": csd.id, "label": csd.name} for csd in country_subdivisions]
            country_subdivisions = json.dumps({"country_subdivisions": country_subdivisions})

        return HttpResponse(country_subdivisions)
    else:
        return HttpResponse(json.dumps("{}"))

这里发生了一些事情:

功能在页面加载

上运行

当页面加载时,$("#id_company_country").change内部的函数似乎正在运行,而下面是错误。

我想我必须确定所以我检查国家/地区值并将其设置为默认值(如果它为空或空两次)。一旦进入功能,一旦进入功能。

if (country == null) {
    country = 31;
}

为了进行比较,我尝试了运算符===,反转!=!==和值null''和“”。他们都没有工作。

因为我仍然得到这个错误,我想当我打开网站时正在调用该函数。

Reverse for 'get_country_subdivisions' with arguments '()' and keyword arguments '{'country_id': ''}' not found.

返回值

为了测试purpuses,我在get函数中将国家/地区硬编码为1

$.getJSON("{% url 'get_country_subdivisions' country_id=1 %}", function(data, status) {
    alert(status)
});

当我这样做时,我会获得具有所选国家/地区价值的警报框,因此这似乎有效。 但是当我得到回复时,它看起来像这样(来自谷歌Chrome控制台):

GET
http://localhost:8012/contact_management/get_country_subdivisions/1/[%22Ala…ashington%22,%20%22West%20Virginia%22,%20%22Wisconsin%22,%20%22Wyoming%22] 404 (NOT FOUND)

这显然不是正确的url,但我希望这些值在data参数中。

我在这里遗漏了哪些明显的事情?

0 个答案:

没有答案