2014-12-23 29 views
1

回車鍵我有下面的代碼創建一個文本框:jQuery的檢測對動態創建文本框

$(".vocab-list").append($('<li style="margin-left: 307px;" class="vocab-word" id="vocab_' + vocabWords[wordId]['id']+ '"><img width="230px" height="230px" src="' + vocabWords[wordId]['imageURL'] + '" /><div><input type="text" spellcheck="false" id="writeWord" autocorrect="off" maxlength="200" style="width: 230px;border: 0;border-bottom: 1px solid #C8C6C6;border-radius: 0;color:#6e572f;font-size:24px;font-weight:500;"></div></li>').hide().fadeIn(600)); 

如何檢測回車鍵?以下沒有工作

$('#writeWord').bind("enterKey",function(e){ 
alert("Enter"); 
}); 
+0

可能重複[使用Javascript拍攝鍵] (http://stackoverflow.com/questions/756786/javascript-capture-key) –

+0

'writeWord'不是一個標準的HTML標籤,你的意思是'.writeWord'?當你創建文本框時看不到'.writeWord'元素 – Neverever

+0

我的意思是#writeWord ...我只需要檢測從dyanmically創建的文本框中的輸入鍵 – Jake

回答

1

使用.on動態HTML - 檢測keyup事件,然後檢測是否的關鍵行動是爲回車鍵:

$(".vocab-list").on("keyup", "#writeWord", function(e) { 
    if (e.which == 13) { 
     alert("Enter"); 
    } 
});