jquery如何在文档就绪上调用函数

时间:2011-11-15 20:33:19

标签: jquery

嗨,有人可以帮我解决这个问题吗?我有:

$(document).ready(function () {

    window.refresh = function () { setorganisationddl; }

    var setorganisationddl = function () {
        if ($("#Invoice_AreaId option:selected").val() == "") {
            $("#Invoice_OrganisationId > option").remove();
            $("#Invoice_OrganisationId").append($("<option value=''>-- Select --</option>"));
            $("#Invoice_OrganisationId").attr("disabled", "disabled");
        }
        else {
            $.get('/Invoice/GetOrganisationSelectList/' + $("#Invoice_AreaId option:selected").val(), function (response) {
                $("#Invoice_OrganisationId").removeAttr("disabled");

                var organisations = $.parseJSON(response);

                var ddlOrganisations = $("#Invoice_OrganisationId");

                $("#Invoice_OrganisationId > option").remove();

                for (i = 0; i < organisations.length; i++) {

                    if (organisations[i].Value == $("#Invoice_OrganisationId option:selected").val()) {
                        ddlOrganisations.append($("<option selected='selected' />").val(organisations[i].Value).text(organisations[i].Text));
                    }
                    else {
                        ddlOrganisations.append($("<option />").val(organisations[i].Value).text(organisations[i].Text));
                    }
                }
            });
        }
    }

    $("#Invoice_AreaId").change(setorganisationddl);
});

所以我在调用id为Invoice_AreaId的ddl时调用了setorganisationddl。大。但是我也想在页面加载时调用它。

如你所见,我试过了:

window.refresh = function () { setorganisationddl; }

哪个不起作用。

3 个答案:

答案 0 :(得分:2)

您只需稍微重新安排一下代码。

在您的文档中准备好setorganisationddl,并在.change处理程序中。

$(document).ready(function () {

    setorganisationddl();



    $("#Invoice_AreaId").change(function(){
         setorganisationddl();
    });
});

function setorganisationddl() {
    if ($("#Invoice_AreaId option:selected").val() == "") {
        $("#Invoice_OrganisationId > option").remove();
        $("#Invoice_OrganisationId").append($("<option value=''>-- Select --</option>"));
        $("#Invoice_OrganisationId").attr("disabled", "disabled");
    }
    else {
        $.get('/Invoice/GetOrganisationSelectList/' + $("#Invoice_AreaId option:selected").val(), function (response) {
            $("#Invoice_OrganisationId").removeAttr("disabled");

            var organisations = $.parseJSON(response);

            var ddlOrganisations = $("#Invoice_OrganisationId");

            $("#Invoice_OrganisationId > option").remove();

            for (i = 0; i < organisations.length; i++) {

                if (organisations[i].Value == $("#Invoice_OrganisationId option:selected").val()) {
                    ddlOrganisations.append($("<option selected='selected' />").val(organisations[i].Value).text(organisations[i].Text));
                }
                else {
                    ddlOrganisations.append($("<option />").val(organisations[i].Value).text(organisations[i].Text));
                }
            }
        });
    }
}

答案 1 :(得分:1)

只需在.ready()处理程序中调用它:

var setorganisationddl = function () {
    ...
}
setorganisationddl();

如果你真的希望在页面完全加载时调用它,而不是在它准备就绪时调用它,你需要在任何ready处理程序之外定义函数。

function setorganisationddl () {
    ...
}
$(window).load(setorganisationddl);
$(document).ready(function () {
    $("#Invoice_AreaId").change(setorganisationddl);
});

答案 2 :(得分:0)

你能做到 -

$("#Invoice_AreaId").change(setorganisationddl).change();

在代码底部,然后移除window.refresh电话。那应该绑定函数然后触发'change'事件。