I have the code below which I use clone() and live().
The code is being load in a jQuery UI dialog with a link.
Whenever I click the link it goes to server and fill the dialog with the code below.
The first time page is being loaded it works fine, but if I close the dialog and click the link again to get the dialog the number of Ajax requests which is being send increases.
The first time I send trigger the change I send only one request, I close the dialog and load it again and then trigger the change, it send two Ajax request at same time, the third time three request at same time and so on.
Where do you think my problem is?
<input id="TaskId" name="TaskId" type="hidden" value="18" />
<div id="MainDiv">
<div id="toClone">
<div style="display: inline;">
<select id="Tasksess">
<option value="">لطفاً کار را انتخاب کنيد</option>
<optgroup label="کار های جديد">
<option value="16"style="">q3fe</option>
<option value="18"style="">fjhv j</option>
<option value="19"style="">wref</option>
<option value="25"style="">ff</option>
</optgroup>
<optgroup label="کار های در دست اقدام">
<option value="13"style="">rr</option>
<option value="15"style="">yy</option>
</optgroup>
<optgroup label="کار های تمام شده">
<option value="14"style="">tt</option>
<option value="18"style="">fjhv j</option>
</optgroup>
</select>
</div>
<div style="display: inline;">
<select id="Statusess" name="Statusess"><option value="">لطفاً وابستگی را انتخاب کنيد</option>
<option value="1">پيشنياز</option>
<option value="2">همنياز</option>
</select>
</div>
<div style="display: none;" id="Ok">
ok
</div>
<div style="display: none;" id="noOk">
تکراری
</div>
<div id="loadingGif" style="display: none;">
<img src="/Content/Images/ajax-loader/253L.gif" alt=""/>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
var Maindiv = $("#MainDiv");
var toClone = $("#toClone");
//$("#Statusess").each(function () {
$("#Statusess").live('change', function () {
if ($(this).find(":selected").val() != "") {
if ($(this).parent().prev().find(":selected").val() != "") {
$(this).parent().parent().find("#loadingGif").attr("style", "display: inline;");
$.ajax({
url: '/ProjectAdmin/Project/AddTaskDependency?MainTaskId=' + $("#TaskId").val() + '&DependentTaskId=' + $(this).parent().prev().find(":selected").val() + '&Status=' + $(this).find(":selected").val(),
type: 'GET',
success: function (data, status) {
if (data != "0") {
$(this).parent().parent().find("#Ok").attr("style", "display: inline;");
$(this).parent().parent().find("#noOk").attr("style", "display: none;");
}
else if (data == "0") {
$(this).parent().parent().find("#Ok").attr("style", "display: none;");
$(this).parent().parent().find("#noOk").attr("style", "display: inline;");
}
var div = $('div:eq(0)', Maindiv).clone();
Maindiv.append(div);
}
});
$(this).parent().parent().find("#loadingGif").attr("style", "display: none;");
}
}
});
//});
});
</script>
as stated above you should use
.delegate()instead of.live().In order to use
delegateall you need to do is specify a parent in which you will be listening to and a selector on which it’s going to act.Try this:
Here you a have a working jsfiddle, also here is a link for reference to .delegate() function.