简体   繁体   中英

Use jQuery to load content into DIV and then manipulate select menu options

After page load, I am loading a select menu into a DIV like this:

$('div#searchform').load('_searchform.asp');

That load generates a SELECT menu which displays on the screen. Afterwards, I am trying to select certain options in that list, but the select menu doesn't exist in the DOM. I know how to use ON to assign events to dynamically-loaded content like this, but in this case there is no event. I just want the "select" to happen on page load. My Javascript looks like this:

$('option[value="185"]').attr('selected', 'selected');

But it doesn't see the menu, so that doesn't work. How can I get my script to see the dynamically-loaded content and manipulate the selected option?

Thank you!

Do the work in the callback:

$('div#searchform').load('_searchform.asp', function() {
    //content loaded, manipulate here
});

Use callback function of load

$('div#searchform').load('_searchform.asp', function() {
    //Peform manipulation here
    $('option[value="185"]').prop('selected', true);
});

Also, Use .prop() instead of .attr()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM