2014-07-18 90 views
-2

现在我只是将我的jQuery库升级到1.8.2,并更改为下面的代码。jQuery 1.8.2上的命令不像生命命令那样工作

$(document).on('change','#frmLocations #cboSearch',function(event){ });

但是当我动态地添加一些DOM如“表”与点击的事件,改变其发明了不能正常工作。但是当我把“活”的命令,它正常工作。所以请帮助我用“开启”命令替换“实时”命令。
其实现在我已经厌倦了这一点。

请检查此链接,当我点击第一个和第二个文本框时,警报应该是“第三”, 但不起作用。 http://jsfiddle.net/6TcPA/4/

$(function(){ 
    $('div').append(
     '<input id="first"/><input id="second"/>' 
    ); 

    $(document).on('click','#first',function(event){ 
     $('#second').attr('id','third'); 
    }); 

    $(document).on('click','#second',function(event){ 
     alert("second"); 
    }); 

    $(document).on('click','#third',function(event){ 
     alert("third"); 
    }); 

    $(document).on('click','#third',function(event){ 
     alert("third"); 
    }); 
}); 

现在2个警报( “第三”)将出现。

+0

请提一下您的html,以便我们可以看到问题是否存在。 –

+0

请你详细说明'不能正常工作'吗?也添加一些相关的代码。 –

+1

你的意思是选择器是“#frmLocations#cboSearch''还是''#frmLocations,#cboSearch''?第二个将针对两个不同的对象,第一个只有一个特定的对象。如果我们看到您尝试定位的实际HTML,我们可以提供更好的帮助。 – jfriend00

回答

0

你应该用逗号分隔事件以下根据OP的要求

更新和新独立IDs.Use fiddle

$(function(){ 
    $('div').append(
     '<input id="first"/><input id="second"/>' 
    ); 

    $('#first').on('click',function(){ 
     $('#second').attr('id','third'); 

    }); 

    $('#second').bind('click',function(){ 
     alert("third"); 
    }); 

});  

或u能做到这样也Fiddle

$(function(){ 
    $('div').append(
     '<input id="first"/><input id="second"/>' 
    ); 

    $('#first').on('click',function(){ 
     $('#second').attr('id','third'); 

     $('#third').bind('click',function(){ 
      alert("third"); 
     }); 

    });    
});  

旧的:

$(document).on('change','#frmLocations , #cboSearch',function(event){ 
}); 

您可以检查herehere其工作。

+0

请检查这个链接,当我点击第一个和第二个文本框,警报应该是“第三”。 但无法正常工作。 http://jsfiddle.net/6TcPA/4/ // HTML

// java描述 $(函数(){ $( 'DIV')。追加( “<输入的ID =“第一('click','#first',function(event){('#second')。prop(('#second'/>' ); $ 'ID')= '第三'; }); $(文件)。在( '点击', '#第二' 功能(事件){ 警报( “第二”); }); $(文件)。在( '点击','#第三',功能(事件){alert(“third”); }); }); –

+0

@RoshanPerera,看看这个小提琴http://jsfiddle.net/6TcPA/7/。它改变原始div的编号(TWO => THREE)。但事件不会改变。因此,分配事件点击ID为“2”的元素,但即使id变为“三”,事件(点击)仍然是相同的。 –

0

你可以试试这个:

$(document).on('click','#first,#second',function(event){ 
     alert("sdfdsf"); 
    }); 

相反的:

$(document).on('click','#first #second',function(event){ //wrong id should be comma seprated 
     alert("sdfdsf"); 
    }); 

Working DEMO

注:如果动态内容是select box那么你可以使用变化event如果你的input可以使用keyup事件。如果我能看到你的标记,然后能够给出更具体的答案。

+0

检查这个http://jsfiddle.net/6TcPA/4/ –