jQuery 无法使用 .on() 将事件绑定到 AJAX 加载的元素
使用 .load
,我将以下内容加载到 #step_2
div 中:
<div id="step_2">
Select a subcategory:
<select id="subcategory_id" name="subcategory_id">
<option value="0">All subcategories</option>
<option value="68">Cool</option>
<option value="15">Cooler</option>
<option value="74">Excellent</option>
</select>
</div>
然后我尝试检测用户何时选择某些内容:
$("#subcategory_id").on("change", function(){
alert( 'Success!' );
});
但没有成功。有人看到我做错了什么吗?非常感谢帮助。
注意:
这是 FireBug 看到的完整 CSS 路径:
html.js body div#container div#main form div#step_2 select#subcategory_id
如果上下文很重要,这是完整的 Javascript 文件:
$(document).ready(function() {
$("#category_id").change(function() {
$("#spinner").show();
$("#step_2").load("process.php?selected=category", { value: $(this).val() }, function() {
// Do something to say the data has loaded sucessfully
$("#spinner").hide();
});
});
$("#subcategory_id").on("change", function(){
alert( 'Success!' );
});
});
Using .load
, I'm loading the following content into #step_2
div:
<div id="step_2">
Select a subcategory:
<select id="subcategory_id" name="subcategory_id">
<option value="0">All subcategories</option>
<option value="68">Cool</option>
<option value="15">Cooler</option>
<option value="74">Excellent</option>
</select>
</div>
I'm then trying to detect when user selects something:
$("#subcategory_id").on("change", function(){
alert( 'Success!' );
});
but with no success. Does anyone see what I'm doing wrong? Help greatly appreciated.
NOTES:
Here's the full CSS path as seen by FireBug:
html.js body div#container div#main form div#step_2 select#subcategory_id
Here's the full Javascript file, if context matters:
$(document).ready(function() {
$("#category_id").change(function() {
$("#spinner").show();
$("#step_2").load("process.php?selected=category", { value: $(this).val() }, function() {
// Do something to say the data has loaded sucessfully
$("#spinner").hide();
});
});
$("#subcategory_id").on("change", function(){
alert( 'Success!' );
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在这种情况下,您需要以与
delegate
类似的方式使用on
,您使用的语法是bind
的替代。第一个选择器必须是不会被替换的元素。这样,当事件冒泡到这一点时,它就会被捕获。然后,您将触发事件的实际元素指定为
.on
的第二个参数。In this case you need to use
on
in a similar way todelegate
, the syntax you are using is the replacement forbind
.The first selector has to be an element that won't be replaced. This way, when the event bubbles up to this point it is caught. You then specify the actual element that will trigger the event as the 2nd parameter of
.on
.如果没有正确的设置,
on(...)
不会订阅页面上尚未出现的事件。要执行您想要的操作,您应该尝试以下操作:为了提高效率,您应该将
$(document)
替换为您知道在执行此代码时将出现在页面上的元素的最接近的父元素被称为。on(...)
won't subscribe to events that aren't on the page yet without the proper setup. To do what you want, you should try the following:To make this more efficient, you should replace
$(document)
with the closest parent of the element that you know will be on the page when this code is called.您必须在加载 html 后绑定处理程序,或者使用
You have to bind the handler after you've loaded the html, or use
当然可以,
要使用 .on 委托事件,请提供要绑定的父元素,然后提供要委托的选择器。
Sure it can,
To delegate events using .on, provide a parent element to bind to, and then the selector to delegate to.
您必须在加载新元素后设置事件。由于 AJAX 是异步的,因此您需要在加载中具有回调。
You have to set the event after that you have loaded the new element. As AJAX is asynchronous, you need to have a callback in your load.