2014-02-28 49 views
0

我一直在瀏覽堆棧溢出的答案,我不能完全找到我的代碼的錯誤。使用切換評論部分

我目前正在嘗試爲我的評論部分(顯示爲輸入表單)創建一個切換功能,因此它根據點擊按鈕隱藏/顯示。這裏是我的代碼:

$("<button />", { 
    "class": "btn btn-primary", 
    "text": "Show Comments", 
    "on-click": "showhidecomments()" 
}).appendTo(wrapper); 

$("form />",{ 
    "class": "comment-sec", 
    "placeholder": "Add comments here", 
    "input type": "text", 
    "input type": "submit", 
    "display": "none" 
}).appendTo(wrapper); 

// Show/hide comment box with input fields 
var showhidecomments = function() { 
('.comment-sec').toggle(); 

}; 

我不斷收到控制檯上的錯誤是:遺漏的類型錯誤:對象的.comment秒有沒有方法「切換」

我不認爲我使用切換錯誤 - 爲什麼這會導致對象錯誤?

+2

缺少'$'..'$('。comment-sec')。toggle();' –

回答

0

你缺少$

var showhidecomments = function() { 
    $('.comment-sec').toggle(); 
//^missing here 
}; 

有幾個問題,如

$("<button />", { 
    "class": "btn btn-primary", 
    "text": "Show Comments", 
    "onclick": "showhidecomments()" //should be onclick 
}).appendTo(wrapper); 

//it should be <form> not form> 
$("<form />", { 
    "class": "comment-sec", 
    "display": "none" 
}).hide().appendTo(wrapper); 

演示:Fiddle

此外,我會建議使用jQuery來添加單擊處理程序,而不是使用內聯屬性

演示:Fiddle

+0

謝謝!它在jsfiddle上完美工作 - 評論框仍然沒有出現在我的代碼中,但我認爲必須有其他影響(與此問題無關)。 – user3344449