shanish
shanish

Reputation: 1984

textbox change event - jquery

I have a textbox control, the script is

<asp:TextBox ID="txtDate" EnableTheming="false" CssClass="calender" 
                        runat="server" ClientIDMode="Static"></asp:TextBox>

in the calender control if I select a particular date I need to get the records from the database on that particular date, so I tried putting textbox change event as

$(function () {
    $('#txtDate').change(function () {
        $.ajax({
            type: "POST",
            url: "/DataService.asmx/fetchAbsentees",
            contentType: "application/json; charset=utf-8",
            data: "{'date' : '" + $("#txtDate").val() + "'}",
            dataType: "json",
            success: function (data) {
                alert("Successfully reached.");
            },
            error: function (req, status, error) {
                alert("ERROR:" + error.toString() + " " + status + " " + req.responseText);
            }
        });
    }).triggerHandler('change');
});

the webservice is getting fired from here fine, but its firing whenever I run the application, I want it to be fired only when I change the date in the textbox......can anyone help me here.

note: am using master page.

Upvotes: 0

Views: 10829

Answers (1)

musefan
musefan

Reputation: 48415

You need to remove the following part:

.triggerHandler('change');

this causes the event to fire immediately.

Your final code should look like the following:

$(function () {

    $('#txtDate').change(function () {
        $.ajax({
            type: "POST",
            url: "/DataService.asmx/fetchAbsentees",
            contentType: "application/json; charset=utf-8",
            data: "{'date' : '" + $("#txtDate").val() + "'}",
            dataType: "json",
            success: function (data) {
                alert("Successfully reached.");
            },
            error: function (req, status, error) {
                alert("ERROR:" + error.toString() + " " + status + " " + req.responseText);
            }
        });
    });

});

Upvotes: 3

Related Questions