首页 > 解决方案 > 单击按钮以使用 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 服务器,用于将数据记录到控制台。当我单击按钮时,页面消失。造成这种情况的原因是什么?而且,我该如何解决这个问题?

标签: javascripthtmljquery

解决方案


你有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()


推荐阅读