2017-04-22 17 views
0

我在我的項目上使用backbone,jquery和handlebars。JQuery更改表上的一行

我試圖改變行類只是點擊。
我的意思是如果我點擊編輯按鈕(表的右邊),它會改變類。

看看錶格截圖瞭解。

enter image description here

AdminTableView.js

var addAdminPanelView = Backbone.View.extend({ 

    el:$(".page"), 
    events:{ 
     'click #editButton':'editEvent', 
     'click #deleteButton':'deleteEvent' 
    }, 


    editEvent:function(){ 
     console.log("edit"); 
     this.$el.children().find("th").addClass("info"); 
    }, 

我的模板

<tbody id="myTable"> 
    <tr class="active"> 
    {{#travels}} 
    <th>{{user.department.departmentName}}</th> 
    <th>{{user.managerID}}</th> 
    <th>{{user.firstName}} {{user.lastName}}</th> 
    <th>Seyahat Başlangıcı</th> 
    <th>Seyahat Sonu</th> 
    <th>{{location}}</th> 
    <th>{{travelPurpose}}</th> 
    <th>{{travelCost}}</th> 
    <th>{{projectCode}}</th> 
    <th> <a href="/#travel"> 
     <span id="deleteButton" class="glyphicon glyphicon-minus-sign"></span> 
    </a> 
     <a href="#"> 
      <span id="editButton" class="glyphicon glyphicon-edit"></span> 
     </a> 
    </th> 
    {{/travels}} 
    </tr> 
</tbody> 

如果我點擊任何編輯按鈕,所有的行添加類 「信息」

回答

1

似乎事件,如果點擊「編輯」按鈕點擊。

但你必須知道在哪一行上。
在「event.target」中使用.target可能有助於查找父行。

試試這個:

editEvent:function(e){ 
     console.log("edit"); 
     $(e.target).closest("tr").addClass("info"); 
    }, 
+1

由於這是工作! – real

+0

我們可以聊天,如果你想知道它爲什麼有效。 ;) –

0

試試這個$("editButton").parent(".active").addClass("yourClass");或此 $("editButton").parent("tr").addClass("yourClass"); 我希望它可以工作