jQuery Dialog - Array defined in dialog is not removed on close
I'm facing a typical problem with jQuery dialog.
I am basically doing some javascript operations after which I am updating a simple JS array in the dialog. A "process" button will pass the array values to the server. If I click "close" on dialog the dialog is simply removed.
However, upon re-creating the dialog, the array of the previous session remains i.e it still has some size. It is not removed along with the dialog.
I tried everything. For ex: in the parent page:
close: function()
{
$("#dialog"开发者_开发知识库).remove();
array.length=0;
array=[];
}
Or within the dialog during script initialization:
$(document).ready(function() {
var array = new array();
array.length=0;
}
Or even trigger a call when I click the dialog close button:
$(".ui-dialog-titlebar-close").click(function(){
*remove array*
});
Nothing. Zero. Zippo. The array remains. As you may have guessed, this is playing havoc with my server updates and I'm not sure what the problem may be.
If anyone of you has a solution, I'd be much obliged.
Thanks a lot.
Try this it working for me
<script type="text/javascript">
var array = new Array();
$(function () {
$("#dialog").dialog({
autoOpen: false,
buttons: {
"Cancel": function () { $(this).dialog("close"); },
"Ok": function () { $(this).dialog("close"); }
},
close: function () { alert(array); array = new Array(); },
open: function () { alert(array); }
});
$("#showDialog").click(function () {
$("#dialog").dialog("open");
});
$("#updateArray").click(function () {
array = [1, 2, 3, 4, 5]
});
});
</script>
Html:
<input type="button" id="showDialog" value="Show Dialog" />
<div id="dialog">
Aliquam elementum ut? Tincidunt mattis in, ac ut, a cursus integer, habitasse? Elementum
proin? Sit amet et elementum pulvinar integer, non, tortor dis in. Pulvinar dictumst!
Turpis. Ultricies augue habitasse? Hac nunc integer risus enim ac? Ut nisi nisi
magna velit elementum diam auctor purus et amet a!
<br />
<input type="button" id="updateArray" value="Update Array" />
</div>
When I try it, it works.
here is my snippet:
window.onload = function () {
var array = ['foo', 'bar'];
console.log(array);
array = [];
console.log(array);
}
the first output contains the elements. the second output is empty. maybe the problem is, that you have to say array = new Array() instead of "new array()". but using the literals [] definitly works for me.
精彩评论