Reputation: 1144
The basics of the functions work, but for every cancel button thats clicked, the next button will add that number of clicks to it. For example if a user clicks a cancel button once, then next time a different button with a different id is clicked, firebug shows the page that contains the form (page2) to load that many times, instead of once. This all generated dynamically, so there may be up to 10 of these div's on the parent page. What should happen is:
In the 1st page I have:
<head>
<script type="text/javascript">
$(document).ready(function(){
var ajaxInProgress = false;
$(function(){
if (ajaxInProgress) return;
$('.ce').focus(function(){
var cid = this.id;
$('#comfield'+cid).empty();
$('#comfield'+cid).load('content_comment.php?id=<%=intmemberid%>&cid=' + cid);
});
});
</script>
</head>
<body>
<div id="comform"
<div id="comfield2">
<input class="ce" id="2" type="text" value="Write a comment..." readonly="readonly" />
</div>
</div>
<div id="comdata2"></div>
<div id="comform"
<div id="comfield3">
<input class="ce" id="3" type="text" value="Write a comment..." readonly="readonly" />
</div>
</div>
<div id="comdata3"></div>
</body>
In page 2 I have
<script type="text/javascript">
$(document).ready(function() {
document.commentform2.comment.focus();
$("#cancelcomment2").click(function(){
$('#comfield2').empty();
$('#comfield2').html('<input class="ce" id="2" type="text" value="Write a comment..." readonly="readonly" />');
var ajaxInProgress = false;
$(function(){
$('.ce').focus(function(){
if (ajaxInProgress) return;
var cid = this.id;
$('#comfield'+cid).empty();
$('#comfield'+cid).load('content_comment.php?id=55&cid=' + cid);
});
});
});
$("#submitcomment").click(function(){
$('#formcomment').ajaxForm(function (data, textStatus){
$('#formcomment').clearForm();
$('#comfield2').empty();
$('#comfield2').html('<input class="ce" id="2" type="text" value="Write a comment..." readonly="readonly" />');
$('#comdata2').append(data).fadeIn(700);
var ajaxInProgress = false;
$(function(){
if (ajaxInProgress) return;
$('.ce').focus(function(){
var cid = this.id;
$('#comfield'+cid).empty();
$('#comfield'+cid).load('content_comment.php?id=55&cid=' + cid);
});
});
});
});
});
</script>
<form id="formcomment">
<textarea id="commenttext2>" name="comment" class="commentarea"></textarea>
<input type="submit" id="submitcomment" value="comment />
<button id="cancelcomment2">Cancel</button>
</form>
Upvotes: 0
Views: 647
Reputation: 95022
This may not be the "best" way to do it, but it will work.
replace all occurences of
$('.ce').focus(function(){
with
$('.ce').unbind("focus").focus(function(){
in both scripts.
The better way would be event delegation, though that will take more changes to the code.
Upvotes: 2