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.

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();
});
Normally this happens because you are binding an event handler within another event handler. For instance if you were binding a
pagechangeevent handler inside of apageshowevent 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: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
pagechangeevent is firing, a new event handler is bound to the#nameelement.Try this:
This uses event delegation to bind the event handler to the
#nameelement, this way the event handler will be bound once for all-time.Docs for
.delegate(): http://api.jquery.com/delegate