如何在动态元素上触发? [英] How to trigger even on dynamic element?

查看:72
本文介绍了如何在动态元素上触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些由ajax请求创建的单选按钮,其用法如下:

I have some radio buttons that created by ajax request using something like this:

document.getElementById("clientresult").innerHTML=xmlhttp.responseText;

单选按钮是从另一个文件输出的. 如何使用$('input:radio').change()触发选择这些单选按钮时的事件?

The radio buttons are output from another file. How can I use $('input:radio').change() to trigger events on selecting these radio buttons?

推荐答案

这个问题已经被问了很多遍了,但是你去了

This has been asked and answered many many times but here you go

要使on()处理尚不存在的事件,它需要绑定到将要作为将来元素的父级的事件.

For on() to work on events that yet don't exist, it needs binding to one that does that will be a parent of those future elements.

$(document).on('change', 'input:radio', function(){
    // whatever you want to happen
});

document是万能的,但我认为绑定到具有较少搜索节点的对象的速度更快(如果这是正确的术语),例如

document is catch-all but I believe it's faster to bind to something with fewer nodes to search (if that's the right terminology) e.g.

<div id="clientresult">
    <!-- inputs will be created in here -->
</div>

$('#clientresult').on('change', 'input:radio', function(){
    // whatever you want to happen
});

这篇关于如何在动态元素上触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆