javascript - 单击按钮以使用 jQuery 将数据发送到服务器时网页消失
问题描述
我遇到了 HTML 和 jQuery 编程问题。我正在用 HTML 和 jQuery 制作一个公告板,带有 2 个文本框和一个按钮。
$(document).ready(function(e) {
$('save').click(function() {
const name = $('name').val();
const words = $('words').val();
$.post(
"http://localhost:8000/board_write",
{
name: words
},
function(data, status) {
}
)
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="0">
<tr>
<td class="vert" bgcolor="">
Name
</td>
<td>
<input class="name">
</td>
</tr>
<tr>
<td bgcolor="">
Anything you'd like to say
</td>
<td>
<textarea class="words" cols="40" rows="5" placeholder="please enter anything">
</textarea>
</td>
</tr>
<tr>
<td>
<input class="save" type="button" onclick="write()" value="Save">
</td>
</tr>
</table>
而且,我还使用 jQuery 编码将两个文本框的数据发送到 localhost:8000,这是一个 node.js 服务器,用于将数据记录到控制台。当我单击按钮时,页面消失。造成这种情况的原因是什么?而且,我该如何解决这个问题?
解决方案
你有onclick = "document.write()"
; 明确删除文档。https://www.w3schools.com/jsref/met_doc_write.asp
说明: 中的代码onclick
范围如下{window{document{element{}}}}
:如果您打算实现一个write()
函数,请执行此操作并通过 调用它window.write()
,或者将其命名为document.write
. 否则,write()
将 de 解决document.write()
。
推荐阅读
- c# - 具有动态策略提供程序授权的 .NET Core 多重身份验证
- ruby-on-rails - Ruby:如何记录对象并将其重用于测试?
- angular - 使用实体适配器 ngrx 创建子减速器
- python-3.x - 在 psycopg2 中使用循环查询和数据元组的更新语句失败
- mysql - 所以我试图了解 sql 中的 Exists 条件。我知道如果存在一个或多个元组,它会返回 True
- python - 使用现有函数,如何定义具有不同默认可选参数的新函数?
- python - 根据 output2 值从 output1 打印行
- typo3 - TYPO3 - 如何通过前端用户登录限制所有页面,但登录页面本身
- r - 隐藏复选框组选项之一,但仍保持其功能处于激活状态
- javascript - 为什么 backgroundClass 在 mat-paginator 中不起作用?