为什么'我的一次性活动不起作用

Why aren't my onclick events working?

本文关键字:一次性 活动 不起作用 我的 为什么      更新时间:2023-09-26

下面是我在jsfiddle上的一段代码。

我希望"+创建论坛"按钮显示表单,"取消"按钮重置表单并隐藏它,如果你想查看表单,请删除"style="display:none"。

我用了很多w3schools的例子,我试着用它所有的例子来写一个show和hide,其中一个重置成功了,但它是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>

试试这个:

  
$(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>

您使用的是jquery,并且没有将任何jquery库链接到html。

尝试在head标签中链接这个,并再次运行您的代码

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

也许,您已经完成了,jquery库的包含就在您的脑海中。。。但最好先检查一下。。。

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

此外,在正文中混合JS和HTML并不是一个很好的做法。相反,我建议将JS放在标题部分,并在文档准备好后触发代码:

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

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

您正在尝试运行jQuery代码,但似乎您的代码中没有包含jQuery,也没有包含在您提供的fiddle中。请将此添加到您的头标签中

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

再试一次,让我知道。我已经修改了你的小提琴,请看一看-更新的小提琴