iT邦幫忙

2

ajax + aspx(入門)-call page/function

如果input text有修改的話,就呼叫後端程式

1.引入jQuery
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
2.建立Input
<input type="text" id="datepicker" runat="server">
3.Input value如果change,就呼叫後端function

            
            <script>
                $("#datepicker").on("change ", function () {
                    alert($(this).val());
                    var dataValue = "{ name: 'person', isGoing: 'true', returnAddress: 'returnEmail' }";

                    $.ajax({
                        type: "POST",
                        url: "inputonchange.aspx/OnSubmit",
                        data: dataValue,
                        contentType: 'application/json; charset=utf-8',
                        dataType: 'json',
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
                        },
                        success: function (result) {
                            alert("We returned: " + result.d);
                        }
                    });
                });

            </script>

4.後端程式

        //using System.Web.Services;
        [WebMethod]
        public static string OnSubmit(string name, bool isGoing, string returnAddress)
        {
            
            return "it worked";
        }

ref
https://stackoverflow.com/questions/8747439/detecting-value-change-of-inputtype-text-in-jquery
https://stackoverflow.com/questions/18236634/asp-net-jquery-ajax-calling-code-behind-method


尚未有邦友留言

立即登入留言