如何使用.load()加载脚本

时间:2013-01-02 15:53:35

标签: javascript jquery

  

可能重复:
  Why does jQuery or a DOM method such as `getElementByID` not find the element?

我使用jquery.load()加载文件。在我的load_to.html中,我定位ID为

的元素
$('#users').change(function() {
  alert('hello');
});

此元素出现在load_from.html中。我无法针对这一点。但是当我检查页面时,我能够看到这个元素。

我加载了这样的页面

$('#mydiv').load('/user/1/edit form');

如何定位元素?

2 个答案:

答案 0 :(得分:6)

在其委托签名中使用on

$('#mydiv').on('change', '#users', function() {
  alert('hello');
});

Read the docs

答案 1 :(得分:1)

尝试在.load的回调中设置事件,以确保在元素进入DOM后创建它们。

$('#mydiv').load('/user/1/edit form', function () {

    //Callback
    //set up events here (once it is finished loading)

    $('#users').change(function() {
        alert('hello');
    });
});
相关问题