更改事件未触发
我已经克隆了一个表(带有选择)以复制到其他地方。这非常有效,我可以毫无后顾之忧地更改 ID。问题是,如果我尝试将更改事件绑定到每个事件,它永远不会触发吗?这里有明显的错误吗?谢谢。
<script type="text/javascript">
$(document).ready(function () {
var variants = $("table.variantselection").clone(false);
variants.find("select").each(function (i, o) {
$(this).attr("id", $(this).attr("id") + "_popup");
});
variants.find("select").change(function () {
alert($(this).val()); // never reaches this alert
});
variants.appendTo("#popup_variants");
});
</script>
I have cloned a table (with selects) to copy to somewhere else. This works great and I manage to change the ID's with no worries. The problem is that if I try to bind a change event to each of them, will it never fire? Is there a obvious mistake here? Thank you.
<script type="text/javascript">
$(document).ready(function () {
var variants = $("table.variantselection").clone(false);
variants.find("select").each(function (i, o) {
$(this).attr("id", $(this).attr("id") + "_popup");
});
variants.find("select").change(function () {
alert($(this).val()); // never reaches this alert
});
variants.appendTo("#popup_variants");
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 false 更改为 true 以复制具有 DOM 元素的事件处理程序
如果失败,请使用 live() 事件进行绑定:
Change false to true to copy the event handlers with the DOM element
Failing that, use the live() event to bind:
将克隆重新附加到 DOM 后,尝试绑定
.change()
函数。Try binding the
.change()
function after you've reattached the clone to the DOM.由于
#popup_variants
似乎是表格上的父元素,因此您最好使用.delegate()
$(document).ready(function() {
});
Since it appears that
#popup_variants
is the parent element on your table, you might be better off to use.delegate()
$(document).ready(function() {
});