Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Mobile bind events

I have a little problem with jquery mobile. always my page is called this function runs.

$(document).bind('pagechange', function () { 
  // peforms ajax operations
})

The problem is that each time my page is viewed it increases the times my ajax is called... example: if the page is viewed 5 times, next time will peform the same ajax request 6 times.

I'm using asp.Net MVC 4.

Multiple ajax requests

Full code:

@{
    //ViewBag.Title = "Consulta";
    Layout = "~/Views/Shared/_LayoutMenu.cshtml";
}
<div class="ui-body ui-body-b" id="test">
    (...) some html code (...)
</div>
<script>        
$(document).bind('pagechange', function () {
    $('#info').css('visibility', 'hidden');

    $('#name').keypress(function (e) {

        if (e.keyCode == 13) {

            var code = $(this)[0].value;

            $.ajax({
                url: '/Consulta/ObterDadosPulseira',
                data: $(this).serialize(),
                success: function (data) {

                    $('#info').css('visibility', 'visible');

                    var info = $('#info')[0];

                    $('#info [id=gridCod]').html(data[0].cod);
                    $('#info [id=gridName]').html(data[0].nome);

                },
                complete: function () { },
                error: function () { alert('error!'); }
            });

            $(this)[0].value = '';
        }
    });
    $('#name').focus();                       
});

like image 886
Paulo Mendonça Avatar asked Feb 27 '12 18:02

Paulo Mendonça


People also ask

How to bind events in jQuery?

The bind() is an inbuilt method in jQuery which is used to attach one or more event handlers for selected element and this method specifies a function to run when event occurs. event: This is an event type which is passed to the selected elements. data: This is the data which can be shown over the selected elements.

What does jQuery mobile use to handle page events?

However, in jQuery Mobile, Ajax is used to load the contents of each page into the DOM as you navigate, and the DOM ready handler only executes for the first page. To execute code whenever a new page is loaded and created, you can bind to the pageinit event.

How to bind events in JavaScript?

jQuery bind() Method Use the on() method instead. The bind() method attaches one or more event handlers for selected elements, and specifies a function to run when the event occurs.

Which jQuery method is used to attach a handler to an event?

Commonly Used jQuery Event Methods The click() method attaches an event handler function to an HTML element. The function is executed when the user clicks on the HTML element.


1 Answers

Normally this happens because you are binding an event handler within another event handler. For instance if you were binding a pagechange event handler inside of a pageshow event handler.

Also if you want to bind to the page events for a specific page, you can just bind to the data-role="page" element:

$(document).delegate('#my-page-id', 'pageshow', function () {
    //now `this` refers to the `#my-page-id` element
});

Update

I just saw your updated answer with the extra code, and your problem is that you are binding an event handler inside another event handler. Basically each time the pagechange event is firing, a new event handler is bound to the #name element.

Try this:

$(document).delegate('#name', 'keypress', function () {

    if (e.keyCode == 13) {

        var code = this.value;

        $.ajax({
            url: '/Consulta/ObterDadosPulseira',
            data: $(this).serialize(),
            success: function (data) {

                $('#info').css('visibility', 'visible');

                var info = $('#info')[0];

                $('#info [id=gridCod]').html(data[0].cod);
                $('#info [id=gridName]').html(data[0].nome);

            },
            complete: function () { },
            error: function () { alert('error!'); }
        });

        this.value = '';
    }
}).bind('pagechange', function () {
    $('#info').css('visibility', 'hidden');
    $('#name').focus();                       
});

This uses event delegation to bind the event handler to the #name element, this way the event handler will be bound once for all-time.

Docs for .delegate(): http://api.jquery.com/delegate

like image 188
Jasper Avatar answered Oct 01 '22 17:10

Jasper