簡體   English   中英

jQuery事件在流星JS車把內不起作用

[英]jquery event doesn't work inside meteor js handlebars

我必須根據用戶配置文件顯示div,兩個div都按預期方式顯示,但jquery事件(在單獨的文件中)不起作用,但是如果我刪除“ if else condition”,則jquery事件可以正常工作,謝謝。

我試圖將divs放到新模板中,但是它也不起作用。

<!-- 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*/
 });

我建議您改用事件 根據流星命名約定,它可能更容易閱讀。

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*/
  }
});

文檔

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

...

事件類型及其用途包括: 單擊更改焦點模糊 ...

因此,是的,我想說的是,事件應該包含在Template.homeprofile.events ,具體取決於您在問題中定義的前提(嘗試在其中綁定事件)( click )。

否則,如果您確實想將jQuery用於事件,則可以使用on代替click 流星動態呈現內容,這可能就是事件偵聽器未選擇內容的原因。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM