2016-08-03 56 views
0

我必須根據用戶配置文件顯示div,這兩個div如預期般出現,但jQuery事件(在一個單獨的文件內)不起作用,但如果我刪除了「如果其他條件「,jquery事件正常工作,謝謝。jquery事件不起作用流星js把手

我試圖把div放入新模板中,但它也不起作用。

<!-- home profile template --> 
{{# if bandprofile currentUser.profile.type }} 
     <div id="bandnamediv" class="userinforow"> 
     <span>Band name:</span> 
     <input type="text" id="bandnameinput" class="oldinfo bld " value=" {{currentUser.profile.bandName}}"/> 
     <span class="savebandname">save</span> 
    </div> 
{{/if}} 
{{# if userprofile currentUser.profile.type }} 
     <div id="firstnamediv" class="userinforow"> 
      <span class="oldinfo bld">First name: </span> 
      <input type="text" id="firstnameinput" class="oldinfo" value="{{currentUser.profile.firstname}}"/> 
     <span class="savefirstname bld oldinfo">save</span> 
     </div> 
{{/if}} 
<!--end home profile template--> 

模板助手

Template.homeprofile.helpers({ 
    bandprofile: function(usertype){ 
     if(usertype === "band") { return true; } 
     else {return false;} 
    }, 
    userprofile: function(usertype){ 
    if(usertype === "costumer"){ return true; } 
    else {return false;} 
} 
}); 

外部jQuery的文件:

$('.savefirstname').click(function(){ 
    alert('save first name'); 
    /*other things*/ 
}); 
$('.savebandname').click(function(){ 
    alert('save band name'); 
    /*other things*/ 
}); 
+0

是否有任何理由,你試圖使用jQuery而不是助手? – smoksnes

+0

@smoksnes我把所有的jquery技巧放在一個單獨的文件中,所以幫助者不會因這些事件而過度延伸 – nemrrached

+0

因此,使用本地'Template.homeprofile.events'不是一種選擇嗎?不是「幫手」,而是「事件」。 – smoksnes

回答

1

我會建議你使用events代替。根據流星命名慣例,它可能會更容易閱讀。

Template.homeprofile.events({ 
    'click .savefirstname': function(e, template){ 
     alert('save first name'); 
     /*other things*/ 
    } 
    'click .savebandname': function(e, template){ 
     alert('save band name'); 
     /*other things*/ 
    } 
}); 

docs

事件映射是一個對象,其中屬性指定一組事件 的處理,並且該值是針對這些事件的處理程序。

...

事件類型和它們的用途包括:點擊變化重點模糊 ...

所以,是的,我想說的是,事件應該包含在Template.homeprofile.events中,基於您在問題中定義的前提,其中您嘗試綁定一個事件(click)。否則,如果你真的想使用jQuery來處理你的事件,你可以使用on而不是click來代替click。 Meteor動態地呈現內容,這可能是您的事件監聽器沒有選擇它的原因。

$('body').on('click', '.savefirstname', function(){ 
    alert('save first name'); 
    /*other things*/ 
}); 

$('body').on('click', '.savebandname', function(){ 
    alert('save band name'); 
    /*other things*/ 
}); 
+0

它的工作。 (''click','.savefirstname',function(){ alert('save first name'); /*其他的東西*/ });''('。wrapperdiv' – nemrrached