jquery - Explain what is happening in this code -
really stuck trying figure out happening in code below...
function updatecolourdropdown(url) { $("#selectlist").unbind(); $.post(setuniqueparam(url), $("#fruitform").serialize(), function(data) { if (checkexception(data)) { $("#fruitdiv").children().each(function() { removeelem(this); }).end().html(data); $("#selectlist").change(function() { updatecolourdropdown($("#fruitcolourview").val()); }); organiseallocateteams(); } data = null; } ); return false; } basically there form containing 2 dropdown lists, fruit , colour. if user changes fruit in first select list, call server made find out available colours populate second select list.
my html output using jsp..
selectlist = id of select list containing list of fruits,
fruitform = id of form containing select lists
fruitdiv = id of div wraps around 2 select lists
fruitcolourview = id of hidden input field used link struts action (xml)
this working code. trying replicate code on page, i'm finding bit tricky not sure doing, , why... can tell 'data' variable contains entire code page..
i have looked .children .each .end etc etc on jquery website cannot logically put together...
thanks heaps, hope have been clear enough.
function updatecolourdropdown(url) { // remove handlers on #selectlist $("#selectlist").unbind(); // post url specified in parameter, serializing #fruitform $.post(setuniqueparam(url), $("#fruitform").serialize(), // run on success function(data) { // assuming `checkexception` looks in returned // data see if went wrong. if (checkexception(data)) { // went wrong; each of #fruitdiv's children // run function removes child element, // @ end, put data, appears html, // #fruitdiv $("#fruitdiv").children().each(function() { removeelem(this); }).end().html(data); // set new `change` event handler on #selectlist // runs `updatecolourdropdown, given value of // #fruitcolourview. see note 1. $("#selectlist").change(function() { updatecolourdropdown($("#fruitcolourview").val()); }); // call this. organiseallocateteams(); } // useless. data = null; } ); return false; } note 1: in reasonably current versions of jquery i'd handle handler uses event delegation avoid having unbind/rebind repeatedly.
the question has nothing struts, , there's not going on that's particularly difficult reason about.
Comments
Post a Comment