2016-05-08 127 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/