2014-07-18 142 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/ –