2011-11-15 2 views
1

안녕하세요,이 jQuery를 도와 줄 수 있습니까? 내가 가진 : 아이디 Invoice_AreaId와 DDL이 변경 될 때 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 호출되고있다. 큰. 그러나 나는 또한 페이지가로드 될 때 호출하고 싶다. 당신이 볼 수 있듯이

내가 시도 : 작동하지 않는

window.refresh = function() { setorganisationddl; } 

.

답변

2

코드를 약간 재정렬해야합니다.

setorganisationddl에 전화를 걸어 문서를 준비하고 .change 처리기에 연결하십시오. 코드의 맨 아래에

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

하고 window.refresh 호출을 제거 -

$(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

당신은 할 수 없습니다. 그러면 함수를 바인딩 한 다음 'change'이벤트를 트리거해야합니다.

관련 문제