2016-05-08 51 views
-2

這是我的代碼片段jsfiddle爲什麼我的onclick事件不起作用?

我想要「+創建論壇」按鈕來顯示窗體,「取消」按鈕來重置窗體並隱藏它,如果您想查看窗體,請刪除「style =」display:none「

我用了很多例子了W3Schools的和我曾嘗試使用寫節目的所有實例和隱藏,工作的復位之一,但它是一個的onclick =「this.reset()」,這是不我想要的東西,因爲我想它隱藏了。

<html> 
<body> 
<button class="btn btn-default" type="submit" id="createform">+ Create Forum</button> 
<div class="form-group" id="createForum"> 
<form action="forums.php" method="get" id="forumForm" style="display: none"> 
<label>Forum name:</label> 
<input type="text" class="form-control" id="forumName"> 
<label>Description:</label> 
<textarea class="form-control" rows="5" id="description"></textarea> 
<button class="btn btn-danger" type="submit" name="create">Crear</button> 
<input type="button" value="Reset Form" onclick="clearing()"> 
</form> 
</div> 
</body> 

<script type="text/javascript"> 
$("#createform").click(function() { 
$("#forumForm").show(); 
}); 

function clearing() { 
document.getElementById("forumForm").reset(); 
} 
</script> 
+0

您正在使用jquery和香草javascript的混合。此外,我會建議不內嵌JavaScript像你有輸入onclick方法「清除」 – Enjayy

回答

0

試試這個:


 
$(document).ready(function(){ 
 
$("#createform").click(function() { 
 
$("#forumForm").show(); 
 
}); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> 
 

 
<html> 
 
<body> 
 
<button class="btn btn-default" type="submit" id="createform">+ Create Forum</button> 
 
<div class="form-group" id="createForum"> 
 
<form action="forums.php" method="get" id="forumForm" style="display: none"> 
 
<label>Forum name:</label> 
 
<input type="text" class="form-control" id="forumName"> 
 
<label>Description:</label> 
 
<textarea class="form-control" rows="5" id="description"></textarea> 
 
<button class="btn btn-danger" type="submit" name="create">Crear</button> 
 
<input type="button" value="Reset Form" onclick="clearing()"> 
 
</form> 
 
</div> 
 
</body> 
 
</html>

+0

非常感謝,這工作,我錯過了jquery的鏈接,現在我感到非常愚蠢,但謝謝你。 – dreami

+0

@dreami,請爲我的解決方案打勾,如果有幫助;)thanx! – praguan

-1

您正在嘗試運行jQuery代碼,但似乎你已經不包含在你的代碼的jQuery,無論是在您提供的小提琴,請添加該T o您的頭標

<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script> 

再試一次,讓我知道。 我修改了你的小提琴,請看看 - updated Fiddle

0

你正在使用jQuery,你還沒有鏈接任何jQuery庫到你的HTML。

嘗試在頭標籤鏈接這並運行代碼再次

<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script> 
0

也許,你已經做到了和jQuery庫包含在你的頭上......但是最好先檢查一下..

​​

另外,這並不是一個非常好的做法,將JS與HTML混合在一起。相反,我推薦給JS把你的頭部分,讓你的代碼觸發一次您的文檔已準備就緒:

$(document).ready(function(){ 
    $("#createform").click(function() { 
     $("#forumForm").show(); 
    }); 
}); 

https://jsfiddle.net/jy69s6r3/5/