我正在尝试在使用 javascript 单击按钮时显示表单
I am trying to make a form display when button is clicked with javascript
我正在制作一个有留言簿页面的网站。页面上有一个对话区域,顶部有一个链接,上面写着"单击以发布"。我尝试使用 jquery 让它显示允许您添加到页面的表单,但我似乎无法使其工作。 我已经能够让它使用普通的javascript工作,但没有动画,所以我尝试使用.slideToggle(); 但没有成功。有很多代码,我不确定罪魁祸首在哪里,所以如果它真的很长,我很抱歉。 紧跟在脚本标签后面的函数是我最初使用的函数。
以防万一这是一个愚蠢的错误,我不知道 JS/Jquery。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<title>Guestbook -- My Coding</title>
<link rel="stylesheet" href="/css/styles.css" type="text/css">
<script type="text/javascript">
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
$("#display_post").click(function(){
$("#post_new_table").slideToggle();
});
</script>
</head>
<body>
<div id="wrapper">
<?php
$title = '<a href="/guestbook/adminlogin.html" style="color: white; text-decoration: none; cursor: text;">guestbook</a>';
$desc = 'hassle-free conversations';
require('../includes/header.html');
//connect to DB.
//REMOVED THIS PART
/**************************************************************************/
//form to add new post.
if (isset($_POST['n'])) {
echo ($_POST['n']);
}
else {};
?>
<div id="post_new">
<div id='display_post' onclick="">Click to post</div>
<form action="/guestbook/add_post_gb.html" method="POST">
<table id="post_new_table">
<tr>
<td>Name<red>*</red>:</td>
<td><input type='text' name='name' style='width: 400px;' /></td>
</tr>
<tr>
<td>E-Mail<red>*</red>:</td>
<td><input type='text' name='email' style='width:400px;' /></td>
</tr>
<tr>
<td>Message<red>*</red>:</td>
<td><textarea name='message' style='width: 400px; height: 100px;'></textarea></td>
</tr>
<tr>
<td>3*2=<red>*</red></td>
<td><input type='text' name='human_test' style='width: 400px;' /></td>
</tr>
<tr>
<td></td>
<td><input type='submit' name='postbtn' value='POST' />
</tr>
</table>
</form>
</div>
<?php
//print_r($_POST);
/**************************************************************************/
What was here just outputted the already made comments.
</div>
<div style="position: fixed; right: 8px; bottom: 3px;"><a href="#">^Back to top^</a></div>
</body>
你正在将一个 click 事件附加到一个尚不存在的元素,你可以将 JavaScript 移动到 #post_new_table 元素下面,或者你可以将脚本放在 $( document ).ready() 调用中。
$(document).ready(function(){
$("#display_post").click(function(){
$("#post_new_table").toggle();
});
});
将
js 包装在一个函数中,如下所示:
<script type="text/javascript">
$(function(){
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
$("#display_post").click(function(){
$("#post_new_table").slideToggle();
});
});
</script>
根据 JQuery API 文档,这应该可以工作:
$("#display_post").click(function(){
$("#post_new_table").toggle();
});
相关文章:
- 如何隐藏按钮单击事件上的占位符
- 从Web服务器下载图像按钮单击使用JavaScript
- 在SweetAlert中传递ASP.NET按钮单击事件
- 单选按钮单击可刷新/更改网站的小区域
- 调用按钮单击事件 ajax 加载的用户控件
- 附加 jQuery 代码以在加载和按钮单击时运行
- 在按钮单击时将图像URL数据显示到弹出框中,而无需禁用背景
- 按钮单击服务器单击
- 触发单选按钮单击,并在页面刷新时记住选择
- 如何使用 javascript 而不是使用控制器中的方法在 rails 中呈现部分按钮单击
- jQuery UI 选项卡 - 将参数设置为下一个/上一个按钮单击
- 如何在 Angular JS 中的按钮单击上添加类
- 当元素上有多个类时触发按钮单击事件
- 如何在 HTML 中的按钮单击上更改图像(IMG URL 每次来自服务器)
- 如何使用javascript刷新我的html页面时清除按钮单击
- 阻止后退按钮 - 单击按钮时忽略
- 如何制作按钮.单击“开始时不运行”
- Rails:尝试在按钮单击时渲染部分
- 在页面加载时调用函数,在按钮单击时再次调用函数,但参数不同
- 如何在服务器端的文本中添加按钮单击事件