C# 如何在OnSelectedChange上启动jquery函数?

C# 如何在OnSelectedChange上启动jquery函数?,c#,jquery,asp.net,C#,Jquery,Asp.net,我正在我的页面上创建一个动态下拉列表。当从下拉列表中选择一个选项时,如何让它启动jquery函数 Jquery: <script type="text/javascript"> $(document).ready(function () { function insertIntoReporting(_storeID, _personID) { $.

我正在我的页面上创建一个动态下拉列表。当从下拉列表中选择一个选项时,如何让它启动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>
你期待的事件

它看起来像这样:

$('#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;
}

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

将dropdownlist存储在代码隐藏中的受保护字段中,然后:

$('<%=_ddl.ClientId%>').change(insertIntoReporting);
还有另一个选择(我个人更喜欢)是向您希望具有此行为的所有DropDownList添加一些类,并使您的javascript块包含一行,该行按类选择所有这些类:

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

但是我的页面上有多个下拉列表,其中有多个ID。我该怎么做呢?给它们添加一个类,并以它们为目标。因此,它不是
$('#DropdownlistID')。更改将是
$('.DropDownListItem')。更改
或您希望分配给下拉列表的任何类。您也可以只针对`选择标记,但这将侦听页面上所有下拉列表的事件
$('.report-insert-dropdown').change(insertIntoReporting);