gpt4 book ai didi

javascript - 如何将复选框值从 HTML 发送到 Node JS?

转载 作者:搜寻专家 更新时间:2023-10-31 23:48:05 25 4
gpt4 key购买 nike

我尝试将一些复选框值从 HTML 发送到 Node JS。在我的 HTML 文件中,我在表格中有一些复选框。我得到了如下选中的复选框值,

$("#createSS").click(function (event) {
event.preventDefault();
var searchIDs = $("#imgTable input:checkbox:checked").map(function () {
return $(this).val();
}).get();
console.log("selected::::" + searchIDs);
});

我的 HTML 表单是,

<form action="/addSlideShow" method="POST">
<table id="imgTable" class="table">
{{#images}}
<tr>
<td><input id={{imgURL}} type="checkbox" name="ch" value={{imgURL}}/></td>
</tr>
{{/images}}
</table>

<input id="createSS" type="submit" value="Create Slide Show" class="btn btn-success pull-left" disabled/>&nbsp;&nbsp;

</form>

在 Node JS 中,

app.post('/addSlideShow', function (req, res) {
var $ = req.body;
$('td').each(function () {
console.log($(this).text());
});
});

当我点击 HTML 中的按钮时,for 没有提交。我该如何解决这个问题?

提前致谢!

最佳答案

这是因为您没有将数据发布到表单 url。由于您使用了永远不会提交的 event.preventDefault(),您也没有使用 $.ajax()发布数据

尝试使用 ajax 发布数据,

$("#createSS").click(function (event) {
event.preventDefault();
var searchIDs = $("#imgTable input:checkbox:checked").map(function () {
return $(this).val();
}).get();
console.log("selected::::" + searchIDs);
$.ajax({
url:'/addSlideShow',type:'post',
data:{searchid:searchIDs},
success:function(response){
console.log(response);
}
});
});

在node js中你会得到,

app.post('/addSlideShow', function(req, res) {
console.log(req.body); //Output=> like { searchid: 'Array of checked checkbox' }
console.log(req.body.searchid); // to get array of checked checkbox
});

关于javascript - 如何将复选框值从 HTML 发送到 Node JS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29072382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com