admin管理员组文章数量:1333616
I have a ment system where a user submits a ment, the ment is processed, then the HTML for the ment is returned. jquery then adds that retrieved HTML to the ment system. that whole system works, but the ment buttons that requir javascript do not work unless I refresh the page. How do make my javascript work on elements added through load, prepend, or append?
Not sure if my question is clear, but here's the javascript I have:
$(function () {
$(".replyform").submit( function (event) {
event.preventDefault();
id = $(this).attr("id").split('_')[1];
text = $('textarea#text_'+id).val();
$.post( "/api/add/ment/", {_csrf: _csrf, id: id, text: text, a: a},
function (data) {
$('#mentreplies_'+id).prepend(data);
$('#replyform_' + id).hide();
});
});
});
I then have elements such as "reply" for each ment that have functions in an external javascript that do not work unless I refresh the page. Hopefully that made sense.
I have a ment system where a user submits a ment, the ment is processed, then the HTML for the ment is returned. jquery then adds that retrieved HTML to the ment system. that whole system works, but the ment buttons that requir javascript do not work unless I refresh the page. How do make my javascript work on elements added through load, prepend, or append?
Not sure if my question is clear, but here's the javascript I have:
$(function () {
$(".replyform").submit( function (event) {
event.preventDefault();
id = $(this).attr("id").split('_')[1];
text = $('textarea#text_'+id).val();
$.post( "/api/add/ment/", {_csrf: _csrf, id: id, text: text, a: a},
function (data) {
$('#mentreplies_'+id).prepend(data);
$('#replyform_' + id).hide();
});
});
});
I then have elements such as "reply" for each ment that have functions in an external javascript that do not work unless I refresh the page. Hopefully that made sense.
Share Improve this question asked Nov 17, 2011 at 21:26 Jonathan OngJonathan Ong 20.4k18 gold badges82 silver badges118 bronze badges3 Answers
Reset to default 3Use jQuery live() (it is deprecated, see on()) function
jQuery has a live
method to allow elements that are added on the page after loading to be able to have events already bound by jQuery. You can bind your events using live method as described here.
A second solution, and probably a more efficient one, would be using delegate
method to handle events by existing containers and delegating them to the elements inside that container. You can read more about delegate here.
An example solution using live
method is as follows assuming you have buttons with class 'reply' in your response data:
$(".reply").live('click', function(event) {
event.preventDefault();
id = $(this).attr("id").split('_')[1];
text = $('textarea#text_'+id).val();
// post won't work since url is missing, but that code remains the same.
// Assuming you get a response like this
// <div><input type="textarea" id="text2" /><input type="submit" id="reply_2" value="submitReply" class="reply" /></div>
// And if you append this to your document
var data = $('<div></div>').html('<input type="textarea" id="text2" /><input type="submit" id="reply_2" value="submitReply" class="reply" />');
$('#mentreplies_'+id).prepend();
$('#reply_' + id).hide();
});
There are few different approaches to this
1) Explicitly init the button inside returned HTML on AJAX success
2) Setup global handler for your button type using jQuery live()
function (replaced by on()
in 1.7)
3) define button handler right in the markup
Which one do you pick is really up to your specific task.
本文标签:
版权声明:本文标题:ajax - Javascript doesn't work on elements added by jquery's load(), prepend(), or append() functions - Stack Ov 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742281174a2446116.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论