简体   繁体   中英

How do I execute inline javascript in HTML returned from an ajax call - JQUERY

very often I need to run some javascript on HTML contents returned from an ajax call. Just dumping it in a div doesn't seem to work. Whats the best way to do it? LIke lets say I return teh following HTML code:

<div><input type="text" id="someID" /></div>
<script language="javascript">
$('#someID').somefunction();
</script>

You should be using success event handlers. For example if you were using load() you'd do something like:

$('#foo').load('/my/ajax/stuff', function() {
    $('#someID').somefunction();
});

For setting up form elements I generally create a setup function and call it on ready and ajax complete

function setup_form() {
    $('.date-picker').datepicker();
}

$(setup_form);
$.ajaxComplete(setup_form);

This will now setup your form elements on document.ready as well as when an ajax request completes.

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