javascript - 将数据发送到后端 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>
您知道导致问题的原因吗?
解决方案
您正在抓取select
element 而不是form
.
将脚本中的 JS 代码更改为以下内容:-
<script>
function sendGenre() {
let data = document.getElementById("genre").value;
let form = document.getElementById("genreForm")
console.log(data);
form.submit();
}
</script>
推荐阅读
- cordova - 启动预先填充目的地的导航应用程序
- html - 摆脱 CSS 中的黑色边框
- azure - Azure Functions 应用程序在设置应用程序设置后产生错误
- angular - Angular 组件在路由到它后未正确创建
- php - 在 PHP5 MySQL 中按升序检索数据
- apache - 如何在 .htaccess 中锁定不需要的主机?
- java - 有没有办法在控制器中初始化一个变量,并在 FXML 中使用它?
- javascript - Angular 7 服务提供在:'root'
- android - 在Android中,为什么aar文件可以加速编译
- javascript - 如何在响应式幻灯片上添加暂停和播放功能