[英]Why does my JS function not work after dynamically adding content to the page?
I have a javascript function that is defined in the beginning of my webpage. 我在网页的开头定义了一个javascript函数。 It is called when a row is clicked in my table.
在我的表格中单击某行时会调用它。
here is my function: 这是我的功能:
$(".info").click(function(){
alert($(this).attr('movieid'));
});
here is my what my table row looks like: 这是我的表格行的样子:
<tr class="info" movieid="123"><td>movie title</td></tr>
now that table is displayed when I first load the page, I can click the row and it will call the function correctly. 现在,当我第一次加载页面时会显示该表,我可以单击该行,它将正确调用该函数。 I am trying to implement a search function for the table.
我正在尝试为表实现搜索功能。 I have the php script return table rows just like the one above, I add them to the table, but when I click the row that is returned from the php script, the javascript function isn't called.
我有php脚本返回表行,就像上面的那一行一样,我将它们添加到了表中,但是当我单击从php脚本返回的行时,没有调用javascript函数。 Does anyone know how I can accomplish this?
有人知道我该怎么做吗? I've had no luck.
我没有运气。
EDIT 编辑
here is my function to get the search results: 这是我获取搜索结果的功能:
$.ajax({
type: "POST",
url: "www.mysite.com/core/search.php",
data: { q: search_string},
cache: false,
success: function(html){
$("table#movies").fadeOut();
$("div#results-container").fadeIn();
$("div#results").fadeIn();
$("div#results").html(html);
}
});
You are adding the element dynamically so it is not registered when you set the click handler. 您正在动态添加元素,因此在设置点击处理程序时不会注册它。
$("body").on("click", ".info", function(e) {
...
});
body
usually works but ideally you'd want a DOM element as close to your selector .info
as possible. body
通常可以使用,但理想情况下,您希望DOM元素尽可能靠近选择器.info
。
You have to do this: 您必须这样做:
$("body").on("click", "tr", function(event){
alert($(this).attr('movieid'));
});
By adding a "container", it will have an effect like .live()
. 通过添加“容器”,它将具有类似于
.live()
的效果。
Use j Query ready event 使用j查询就绪事件
$(document).ready(function() {
$(".info").on("click", "tr", function(event){
alert($(this).attr('movieid'));
});
});
Add .on
on document
because you are adding DOM
to document. 在
document
上添加.on
,因为您.on
DOM
添加到文档中。
Example 例
$(document).on('click',".info",function(){
alert($(this).attr('movieid'));
});
You php script is most likely not adding the class to the table row. 您的php脚本很可能没有将类添加到表行。 Alter it to add that class.
对其进行更改以添加该类。
Thanks for the help from all of you guys. 谢谢大家的帮助。 I was able to figure it out.. @jasen mentioned that you to declare it as a body tag.
我能够弄清楚。.@ jasen提到您将其声明为body标签。 Here was the solution I was able to use:
这是我可以使用的解决方案:
$("body").on("click", ".info", function(event){
alert($(this).attr('movieid'));
});
Thank you Derek as well.. you provided some useful examples for the future. 谢谢德里克(Derek),您也提供了一些有用的示例。
First, specify a selector where all the tr.info
node will be contained, now and in future. 首先,指定一个选择器,现在和将来将包含所有
tr.info
节点。 From your latest edit of the post, I believe that's table#movies
. 从您对帖子的最新编辑中,我相信它是
table#movies
。
Then attach event with on
and specify the children selector tr.info
: 然后将事件附加到
on
并指定子选择器tr.info
:
$('table#movies').on('click', 'tr.info', function () {
alert($(this).attr('movie'));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.