2014-12-29 18 views
0

我有一個表單,當前包含一個表單組和下面的代碼。流星,將事件添加到單選按鈕會禁用它們

<div class="form-group"> 
    <label>Role</label> 
    <div class="radio"> 
    <label> 
     <input type="radio" name="role-options" id="role-foreman" value="Foreman" checked="">Foreman 
    </label> 
    </div> 
    <div class="radio"> 
    <label> 
     <input type="radio" name="role-options" id="role-super" value="Superintendent">Superintendent 
    </label> 
    </div> 
    <div class="radio"> 
    <label> 
     <input type="radio" name="role-options" id="role-admin" value="Superintendent">Admin 
    </label> 
    </div> 
</div> 

這一切都如預期,但只要我的事件處理程序添加到三個輸入,我失去了表單組,這是當按下一個單選按鈕,它成爲唯一入選的默認功能工程一。

目前我在模板的事件處理程序中有以下代碼。

"click #role-foreman": (e) -> 
    console.log e 
    showAddProjects = true 

"click #role-super": (e) -> 
    console.log e 
    showAddProjects = false 

"click #role-admin": (e) -> 
    console.log e 
    showAddProjects = false 

我所尋找的是按鈕的默認行爲仍然存在,但隨後又運行時按下按鈕的其他代碼。

回答

1

CoffeeScript中有隱含的回報,所以你的事件處理程序編譯成JavaScript看起來像這樣:

"click #role-super": function(e) { 
    var showAddProjects; 
    console.log(e); 
    return showAddProjects = false; 
} 

所以,你的事件處理程序返回false。當發生這種情況時,它stops the event from propagating and prevents the default behavior。爲了解決這個問題,只需添加一個return到你的函數結束,像這樣:

'click #role-super': (e) -> 
    console.log e 
    showAddProjects = false 
    return 
+0

而且最好是'返回FALSE'或'返回undefined'。此外,我建議在函數的第一行上運行'e.preventDefault()'。 –