jquery没有看到新的元素 [英] jquery doesn't see new elements

查看:185
本文介绍了jquery没有看到新的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个简单的问题:



< $($($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ #tunings)。html(< select id = \TuningSelector> \[..]< / div>);
});

$ (#TuningSelector)。change(function(){
DoSomethingWithTheValue();
});

});

问题是:当仪器更改时,脚本不再响应TuningSelector更改。像JQuery没有看到新的TuningSelector元素...



我需要在JQuery上调用刷新吗?那么它会看到刷新的DOM?

解决方案

一般来说,你想使用 on ,以便它将处理程序应用于与选择器匹配的新元素并委托给DOM中的较高元素。



on('change','#control',function(){
DoSomething();
});

  $ 


I am new to JQuery, so bear with me :).

I have a simple problem:

$(document).ready(function() {

     $("#instrument").change(function() {
         $("#tunings").html("<select id=\"TuningSelector>\"[..]</div>");
     });

     $("#TuningSelector").change(function() {
         DoSomethingWithTheValue();
      }); 

 });

Problem is: When instrument has changed, the script doesn't respond anymore to TuningSelector changes. Like JQuery doesn't see the new TuningSelector element...

Do I need to call a refresh on JQuery or so? So that it sees the refreshed DOM?

解决方案

Generally you want to use on so that it applies the handler to new elements matching the selector and delegate to a higher element in the DOM.

$('body').on( 'change', '#control', function() {
    DoSomething();
});

这篇关于jquery没有看到新的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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