In jQuery, how can I control a dynamically populated element (AJAX) without basing it on an event?

I have the following markup when loading a page:

<select name="sel_billing_address"></select>

After loading the page that is selected, it then fills with some AJAX to be something near:

<select name="sel_billing_address">
  <option value="1">hi</option>
  <option value="2">there</option>
  <option value="3" selected="selected">sally</option>
</select>

I need to grab the selected option from this list. This is usually a simple case:

jQuery('select[name=sel_billing_address] option:selected');

but since it is loaded dynamically, I need to track it using .live () - or in other ways. If I control it with live ('change'), then it works fine, but I need values ​​if the selection box could not be changed.

.live('load') , - select , . .live('load') OPTION, SELECT, , , .

, , , script, - .

.

+3
2

livequery. , .live(), live,

+2

(Global) Ajax Events. ajax, .

// Use the Global Ajax Event "ajaxComplete"
$('select[name=sel_billing_address]').bind('ajaxComplete', function () {
    // Check to see if select has any option elements yet
    if ($(this).find('option').length > 0) {
        // Unbind the ajax event
        $(this).unbind('ajaxComplete');

        // Do your stuff
        // Here...
    }
});

jquery, , , "ajaxComplete" "".

EDIT: ... , , .

+1

Source: https://habr.com/ru/post/1764479/


All Articles