1👍
If you want to keep from having the JavaScript in the AJAX response, you could have the completion event of .load()
take the responsibility of setting up the click events:
var url = "/search/friends/?ajax&q="+encodeURIComponent(q)+"&search_type="+encodeURIComponent(type);
$("#results").load(url, function() {
var csrf_token = "{{ csrf_token }}";
$(".user_link").bind('click',function() {
request_friend($(this).id,csrf_token)
});
});
1👍
if I understand your question correctly, you want to make sure click handlers work for links loaded through a later AJAX call?
In jQuery, use the $('.user_link').live('click', function() {})
handler once to assign a click event handler to all current and future links.
👤Max
- [Answered ]-How to test a Django view which has transaction.atomic(using=myDb)?
- [Answered ]-Django: Page not found (404)
- [Answered ]-How to convert the time entered in django admin into utc?
- [Answered ]-How to add script to Django app?
0👍
The Javascript being returned from your AJAX call does not get evaluated unless you specifically tell jQuery to evaluate it. This is done by specifying the dataType
on your AJAX calls, which .load()
doesn’t support.
As seen in this answer, you should use jQuery.get()
or $.ajax
and specify your data type:
$.ajax({
type: "GET",
url: "yourPage.htm",
dataType: "html"
});
- [Answered ]-Understanding VIew evaluation in Django
- [Answered ]-How to create project and application thru the Python with D'jango in Bluemix
- [Answered ]-Filtering on Django Queryset
- [Answered ]-PostgreSQL pg_database_size different from sum of pg_total_relation_size
- [Answered ]-Django UnicodeDecodeError in model object
Source:stackexchange.com