Monday, 15 August 2011

jquery - Referencing $(this) inside each() -



jquery - Referencing $(this) inside each() -

i trying generate table php , place div has class "accordian-table". however, resulting html code not replace div when run code below. assuming incorrectly referencing ".accordian-table", since modified function without each statement works.

function gettable () { $('.accordian-system').each( function() { var accordiansystem = $(this); var selectedid = accordiansystem.children('.selitems option:selected').val(); $.ajax({ type: "post", url: 'ajax-gettable.php', data: { id: selectedid }, success: function(data) { accordiansystem.find('.accordian-table' ).replacewith(data); } }); } ); }

the html construction is:

<div class="accordian-systems"> <h3></h3> <div class="accordian-system"> <select class="selitems"></select> <div class="accordian-table"></div> </div> </div>

edit: guess isn't reaching success function. yet when replace this:

var selectedid = accordiansystem.children('.selitems option:selected').val();

with this:

var selectedid = $('.accordian-system').children('.selitems option:selected').val();

and if remove , closing brace:

$('.accordian-system').each( function() {

this statement won't work properly:

accordiansystem.children('.selitems option:selected').val()

that going match direct children satisfy selector, option:selected never direct kid (it's kid of child) won't match anything. think want this:

accordiansystem.find('.selitems option:selected').val()

if have more 1 element selected, have modify code farther because homecoming array of values, not single value.

also, luis' advice sound. don't assume code doing. set breakpoints, step through , see doing. if not hitting success handler, create error handler , @ arguments see why failing. way code written, passing null selectedid until implement recommended alter above.

jquery html ajax post each

No comments:

Post a Comment