2011-07-18 12 views
1

私は自分のページに動的なドロップダウンリストを作成しています。ドロップダウンからオプションを選択すると、jquery関数を起動するにはどうすればよいですか?どのようにしてOnSelectedChangeでjquery関数を起動できますか?

のjQuery:

  <script type="text/javascript"> 
       $(document).ready(function() { 
        function insertIntoReporting(_storeID, _personID) { 
         $.ajax({ 
         type: "POST", 
         url: "Default.aspx/InsertIntoReporting", 
         data: "{'storeID': _storeID, 'personID': _personID}", 
         contentType: "application/json; charset=utf-8", 
         dataType: "json", 
         async: true, 
         cache: false, 
         success: function (msg) { 
         $('').text(msg.d); 
         } 
         }); 
         return false; 
        } 

       }); 


      </script> 

C#コード

  DropDownList ddl = new DropDownList(); 
      ddl.ID = "DropdownlistID"; 
      ddl.Items.Add("----------"); 
      ddl.DataTextField = "StoreName"; 
      ddl.DataValueField = "StoreID"; 
      ddl.DataSource = ds; 
      ddl.DataBind(); 
      e.Item.Cells[4].Controls.Add(ddl); 


      [WebMethod] 
      public static string InsertIntoReporting(int _storeID, string _personID) 
      { 
       // database calls go here 
      } 

答えて

2

Storeで保護されたフィールドにドロップダウンリストのコードビハインド、そして:

$('<%=_ddl.ClientId%>').change(insertIntoReporting); 

別のオプションをごinsertIntoReporting機能は多くを作ることであろうグローバルにアクセスできるようにしてから、次のようにします。

e.Item.Cells[4].Controls.Add(new LiteralControl(
    string.Format(
     @"<script>$(function() { 
      $('{0}').change(insertIntoReporting); 
     });</script>", 
     ddl.ClientId))); 

さらに、私が個人的に好む別のオプションは、あなたがこの振る舞いをしたいすべてのドロップダウンリストにいくつかのクラスを追加し、それらのクラスによってすべてを選択する行をjavascriptブロックに加えることです:

$('.report-insert-dropdown').change(insertIntoReporting); 
0

jQuery's changeイベントのためにあなたの外観の背後にあります。

それはこのようになります:

$('#DropdownlistID').change(insertIntoReporting); 
function insertIntoReporting(_storeID, _personID) { 
    // $(this) refers to the element that fired the event 
    //In this case $(this).val() should be the equivilant as _storeId 
    //im not sure where _personId is coming from so I will assume its in an input with id of personId 
    var _storeId = $(this).val(); 
    var _personID = $('#personId').val(); 
    $.ajax({ 
     type: "POST", 
     url: "Default.aspx/InsertIntoReporting", 
     data: "{'storeID': _storeID, 'personID': _personID}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     async: true, 
     cache: false, 
     success: function (msg) { 
      $('').text(msg.d); 
      } 
    }); 
    return false; 
} 

+0

しかし、複数のIDを持つページに複数のドロップダウンリストがあります。どうすればいい? –

+1

これらのクラスにクラスを追加し、すべての方法でターゲットを設定します。 '$( '#DropDownListID')。change'の代わりに' $( '。DropDownListItem')。change'やドロップダウンに割り当てるクラスを指定します。 'selectタグをターゲットにすることもできますが、これはページ上のすべてのドロップダウンのイベントを待ち受けます – locrizak

関連する問題