2015-05-22 48 views
0

我有這個html結構。流星兒童輸入內部的事件處理程序

<div class="test"> 
<input type="text" /> <!-- this input is generated autoamitcally so i cant set id or className --> 
</div> 

現在我想使用的輸入,我eventtype selector應該如何看裏面的按鍵事件?

已經與

Template.myTemplate.events({ 
'keypress .test.input .test input':function(event,template){ 
    console.log("and work"); //this dosnt get printed on the console. 
} 
}) 

我知道「按鍵輸入」的作品,但我已經在同一模板的另一個輸入嘗試,所以我想就觸發此輸入的按鍵。

回答

1

如果您在.test div中還有其他輸入(除了會自動生成的輸入),並且它們都不具有ID和CLASS,則無法知道哪個輸入派發了該事件。您必須在每個輸入字段上使用唯一的ID,類或名稱參數。

在另一方面,如果你只有DIV。測試裏面是一個輸入,那麼你正在尋找的事件應該是'keypress .test input' 請參見本MeteorPad例如:MeteorPad

1

對於自動生成的輸入,使用標準的jQuery事件在Templates.sometemplate.rendered中。例如,該代碼會觸發一個事件,每次我按下answerbox內的關鍵在SO:

$('.wmd-container').find('textarea').on('keypress', function(){ 
    console.log('i r texted') 
}) 

在你的情況下,大規模殺傷性武器,容器可以是任何父DIV

相關問題