首页 > 解决方案 > 将数据发送到后端 submit() 不是函数

问题描述

我正在尝试将这个多项选择问题的值发送到后端,但我不明白为什么会收到类型错误

document.getElementById(...).submit is not a function

我仔细检查了没有其他东西被命名为“提交”(很多人都有这个错误)

我试着像这里一样做,但我得到:

createlist:19 Uncaught TypeError: data.submit is not a function
    at sendGenre (createlist:19)
    at HTMLInputElement.onclick (createlist:59)

我的代码是:

function sendGenre() {
  data = document.getElementById("genre").value;
  console.log(data);
  data.submit();
}
<div class="bioContainer">
  <form class="adminPlaylistMainForm" id="genreForm" method="POST">
    <div class="adminPlaylistGenre">
      <p>From what genre do you want to select the music ?</p>
      <select class="adminPlaylistGenreButton" id="genre">
        <option value="all">All</option>
        <option value="jazz">Jazz</option>
        <option value="punk">Punk</option>
        <option value="rock">Rock</option>
      </select>
      <p><input type="button" onclick="sendGenre();" value="Submit form" /></p>
    </div>
  </form>
</div>

您知道导致问题的原因吗?

标签: javascriptforms

解决方案


您正在抓取selectelement 而不是form.

将脚本中的 JS 代码更改为以下内容:-

<script>
  function sendGenre() {
    let data = document.getElementById("genre").value;
    let form = document.getElementById("genreForm")
    console.log(data);
   form.submit();
  }
</script>

推荐阅读