Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Boa noite a todos! :thumbsup:
Então, me deparei com um problema chato e que não estou achando solução.
Tenho um formulário que é submetido por uma div, criada dinamicamente na estrutura a seguir:
<div class="button-container">
<div class="icon save-marker-info" id="save-marker-infoX"> </div>
<div class="icon edit-marker-info" id="edit-marker-infoX"> </div>
</div>
O X corresponde ao índice de um array.
Os eventos seguem, .click():
$('div.save-marker-info').click(function() {
var index = $(this).indexFrom();
$('form#marker-info-form' + index).submit();
return false;
});
.submit():
$('form.marker-info-form').submit(function() {
var index = $(this).indexFrom();
// Debug
$('div#header').append('<span style="color: #F00; font-size: 18px; font-weigth: bold;">Marcador #' + index + ' submetido com os seguintes valores: ' + name.val() + '/' + description.val() + '</span><br />');
var name = $('input#marker-name' + index);
var description = $('textarea#marker-description' + index);
if (name.is(':disabled') || description.is(':disabled')) {
return false;
} else {
if (name.val() != '') {
name.attr('disabled', true);
description.attr('disabled', true);
arrMarker[index].title = hole.address(arrMarker[index]) + '||' + name.val() + '|' + description.val();
$('div.save-marker-info').hide();
$('div.edit-marker-info').show();
} else {
name.focus();
}
return false;
}
});
Sabem me dizer se existe algum problema aparente com este fluxo? :wacko:
A página está no seguinte endereço: http://papoportabela.com/guima/hole/v4/
Para simular o problema, é necessário criar um marcador (com o botão direito do mouse) sobre o mapa. Abaixo, aparecerá uma miniatura do local marcado, passando o mouse, poderá ser visto um ícone para descrever as informações do marcador, clique sobre ele.
Um lightbox é aberto contendo o formulário problemático.
No primeiro instante, funciona perfeitamente, porém se você criar outro marcador, fechando o lightbox e clicando no mapa novamente com o botão direito ou então arrastar o criado anteriormente, já era, o form se torna defeituoso.
Para entenderem, quando você faz uma dessas ações (criar um novo ou arrastar), a lista de marcadores abaixo do mapa é deletada e criada novamente.
Eu coloquei um debug quando se clica no ícone de salvar.
Desde já, agradeço a atenção! :grin:
Procurei pelo método .live() na net e achei isso e um post seu, tentei usá-lo mas não obtive sucesso, o form é submetido várias vezes novamente.
Estou usando corretamente?
$('div.save-marker-info').live('click', function() {
var index = $(this).indexFrom();
$('form#marker-info-form' + index).submit();
return false;
});
Esse método é só para o evento .click(), correto? O .submit() não muda?
Falai, entendi melhor o método e reescrevi meu código. Porém, o problema continua.
A cada ação que chama força o recarregamento dos itens, ele aciona uma vez mais.
Antes eu até imaginava que fosse o registro do evento, que eu fazia a cada reload, porém eu tirei e nada. Não consigo imaginar o que possa ser.
Alguma sugestão?
// Show all created markers
hole.showAll = function() {
$('ul#marker-thumbnail-list').html('');
$('div#marker-info-repository').html('');
for (var i = 0; i < arrMarker.length; i++) {
hole.addThumbnail(i);
}
}
// Add a marker thumbnail
hole.addThumbnail = function(index) {
var latlng = arrMarker[index].position.lat() + ',' + arrMarker[index].position.lng(); '<li class="marker-thumbnail" id="marker-thumbnail' + index + '">' +
'<div class="marker-handler" id="marker-handler' + index + '">' +
'<div class="icon marker-remove" id="marker-remove' + index + '"></div>' +
'<div class="icon marker-zoom" id="marker-zoom' + index + '"></div>' +
'<div class="icon marker-edit" id="marker-edit' + index + '">' +
'<a class="marker-link" id="marker-link' + index + '" href="#marker-info' + index + '"> </a>' +
'</div>' +
'</div>' +
'<div class="marker-thumbnail-static-map">' +
//'<img id="marker-thumbnail-square' + index + '" src="' + hole.staticMap(latlng, 17, '71x71', city.map.getMapTypeId()) + '" alt="Marker#' + index + '" />' +
'<img class="marker-thumbnail-square" id="marker-thumbnail-square' + index + '" src="img/static-map.png" alt="Marcador#' + index + '" title="Teste' + index + '" />' +
'</div>' +
'</li>'
);
hole.addForm(index);
}
// Add a marker form
hole.addForm = function(index) {
var latlng = arrMarker[index].position.lat() + ',' + arrMarker[index].position.lng();
var name = hole.name(arrMarker[index]);
var description = hole.description(arrMarker[index]);
var disabled = '';
if (name != '' || description != '') {
disabled = 'disabled="disabled"';
} '<div class="marker-info" id="marker-info' + index + '" style="display: none;">' +
'<div class="thumbnail-container">' +
//'<img src="' + hole.staticMap(latlng, 17, '300x80', city.map.getMapTypeId()) + '" alt="InfoMarker#' + index + '" />' +
'<img src="img/static-map.png" width="304" height="80" alt="InfoMarcador#' + index + '" />' +
'</div>' +
'<form class="marker-info-form" id="marker-info-form' + index + '" method="post" action="#">' +
'<div class="name-container">' +
'<label class="label" for="marker-name' + index + '"><span style="color: #F00;">*</span> Nome:</label><br />' +
'<input type="text" class="marker-name" id="marker-name' + index + '" value="' + name + '" ' + disabled + ' />' +
'</div>' +
'<div class="description-container">' +
'<label class="label" for="marker-description' + index + '">Descrição:</label><br />' +
'<textarea class="marker-description" id="marker-description' + index + '" ' + disabled + '>' + description + '</textarea>' +
'</div>' +
'<div class="button-container">' +
//'<a href="#" class="save-marker-info button-expandable-clear" id="save-marker-info' + index + '"><span>Salvar</span></a>' +
//'<a href="#" class="edit-marker-info button-expandable-clear" id="edit-marker-info' + index + '"><span>Editar</span></a>' +
'<div class="icon save-marker-info" id="save-marker-info' + index + '"> </div>' +
'<div class="icon edit-marker-info" id="edit-marker-info' + index + '"> </div>' +
'</div>' +
'</form>' +
'</div>'
);
// Register the scripts only once
if (!alreadyMarked) {
hole.registerScript();
}
}
hole.registerScript = function() { mouseenter: function() {
var index = $(this).indexFrom();
$('#marker-handler' + index).css('opacity', '1');
},
mouseleave: function() {
var index = $(this).indexFrom();
$('#marker-handler' + index).css('opacity', '0.3');
}
});
// Tooltip to marker handler
$('div.marker-remove, div.marker-zoom, div.marker-edit').live({
mouseenter: function(e) {
var markerClass = $(this).attr('class').split('-');
var tooltip = '';
if (markerClass[1] == 'remove')
tooltip = 'Remover';
else if (markerClass[1] == 'zoom')
tooltip = 'Zoom';
else if (markerClass[1] == 'edit')
tooltip = 'Descrever';
$('body').append('<div class="tooltip-marker-handler">' + tooltip + '</div>');
$('.tooltip-marker-handler').css({ top: e.pageY - 25, left: e.pageX + 15 }).fadeIn();
},
mouseleave: function() {
$('.tooltip-marker-handler').remove();
},
mousemove: function(e) {
$('.tooltip-marker-handler').css({ top: e.pageY - 25, left: e.pageX + 15 });
}
});
/ INIT - MARKER HANDLER /
// Remove the marker from list and map
$('div.marker-remove').live('click', function() {
var index = $(this).indexFrom();
hole.remove(arrMarker[index]);
hole.showAll();
});
// Set zoom on the clicked marker
$('div.marker-zoom').live('click', function() {
var index = $(this).indexFrom();
hole.focus(arrMarker[index], 17);
hole.infoWindow(arrMarker[index]);
});
// Show the lightbox with textboxes
$('div.marker-edit').live('click', function() {
var index = $(this).indexFrom();
var name = $('input#marker-name' + index);
var description = $('textarea#marker-description' + index);
if (!name.is(':disabled') || !description.is(':disabled')) {
$('div#save-marker-info' + index).show();
$('div#edit-marker-info' + index).hide();
} else {
$('div#save-marker-info' + index).hide();
$('div#edit-marker-info' + index).show();
}
});/ INIT - SAVE INFO /
// When to click to save, submits the respective form
$('div.save-marker-info').live('click', function() {
var index = $(this).indexFrom();
$('form#marker-info-form' + index).submit();
return false;
});
// When submit a form, puts the data in title of the marker and disables the fields
$('form.marker-info-form').live('submit', function() {
var index = $(this).indexFrom();
var name = $('input#marker-name' + index);
var description = $('textarea#marker-description' + index);
$('div#header').append('<span style="color: #F00; font-size: 18px; font-weigth: bold;">Marcador #' + index + ' submetido com os seguintes valores: ' + name.val() + '/' + description.val() + '</span><br />');
if (name.is(':disabled') || description.is(':disabled')) {
return false;
} else {
if (name.val() != '') {
name.attr('disabled', true);
description.attr('disabled', true);
arrMarker[index].title = hole.address(arrMarker[index]) + '||' + name.val() + '|' + description.val();
$('div.save-marker-info').hide();
$('div.edit-marker-info').show();
} else {
name.focus();
}
return false;
}
});
// When click to describe, show the lightbox with textboxes
$('div.edit-marker-info').live('click', function() {
var index = $(this).indexFrom();
var name = $('input#marker-name' + index);
var description = $('textarea#marker-description' + index);
if (!name.is(':disabled') || !description.is(':disabled')) {
return false;
} else {
name.attr('disabled', false);
description.attr('disabled', false);
$('div.edit-marker-info').hide();
$('div.save-marker-info').show();
return false;
}
});// Indicates if the map already was marked
alreadyMarked = true;
}
A página está atualizada neste link: http://papoportabela.com/guima/hole/v4/