2011-11-15 60 views
1

嗨,有人可以帮助我与这个jQuery?我有: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); 
}); 

所以我setorganisationddl正时与DDL ID Invoice_AreaId改变调用。大。不过,我也希望它在页面加载时调用。

正如你可以看到我尝试:

window.refresh = function() { setorganisationddl; } 

不工作。

回答

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

只是把它在你的.ready()处理程序:

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

如果你真的希望它被称为当页面完全加载,而不是只当它准备好了,你需要定义函数在任何ready处理程序之外。

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

你能不能做 -

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

在你的代码的底部,并删除window.refresh电话。这应该绑定该功能,然后触发“更改”事件。