2011-11-15 2 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); 
}); 

のid Invoice_AreaIdとDDLが変更されたときだから私は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'イベントをトリガーする必要があります。

関連する問題