问题是我有一些动态创建的输入标签集,而且我还有一个函数,该函数可以在输入值更改时随时触发。
$('input').on('change', function() {
// Does some stuff and logs the event to the console
});
但是,.on('change')
不会为任何动态创建的输入触发,仅针对页面加载时存在的项目触发。不幸的是,这使我有点束缚,因为.on
它打算替代它,.live()
而.delegate()
所有这些都是包装.bind()
:/
其他人有这个问题或知道解决方案吗?
您应该为on
函数提供选择器:
$(document).on('change', 'input', function() {
// Does some stuff and logs the event to the console
});
在这种情况下,它将按您预期的那样工作。另外,最好指定一些元素而不是文档。
阅读这篇文章可以更好地理解:http : //elijahmanor.com/differences-between-jquery-bind-vs-live-vs-delegate-vs-on/
用这个
$('body').on('change', '#id', function() {
// Action goes here.
});
您可以采用任何一种方法:
$("#Input_Id").change(function(){ // 1st
// do your code here
// When your element is already rendered
});
$("#Input_Id").on('change', function(){ // 2nd (A)
// do your code here
// It will specifically called on change of your element
});
$("body").on('change', '#Input_Id', function(){ // 2nd (B)
// do your code here
// It will filter the element "Input_Id" from the "body" and apply "onChange effect" on it
});
$("#id").change(function(){
//does some stuff;
});
只是为了澄清一些潜在的混乱。这仅在DOM加载中存在元素时才有效:
$("#target").change(function(){
//does some stuff;
});
以后动态加载元素时,可以使用:
$(".parent-element").on('change', '#target', function(){
//does some stuff;
});
$(document).on('change', '#id', aFunc);
function aFunc() {
// code here...
}
您可以使用“输入”事件,该事件在元素获取用户输入时发生。
$(document).on('input', '#input_id', function() {
// this will fire all possible change actions
});
w3的文档
您可以使用:
$('body').ready(function(){
$(document).on('change', '#elemID', function(){
// do something
});
});
它和我一起工作。
本文地址:http://javascript.askforanswer.com/jquery-onchangefunctionbuchufadongtaichuangjiandeshuru.html
文章标签:javascript , jquery
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
文章标签:javascript , jquery
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!