如何将复选框值从HTML发送到Node JS? [英] How to send checkboxes values from HTML to Node JS?

查看:99
本文介绍了如何将复选框值从HTML发送到Node JS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tried to send some checkboxes values from HTML to Node JS. In my HTML file, I have some checkboxes in a table. I got the checked checkboxes values as following,

$("#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不会提交。我该如何解决这个问题?

When I'm clicking the button in HTML, the for isn't submit. How may I fix this?

提前致谢!

推荐答案

这是因为你不是将数据发布到表单网址。因为您使用了 event.preventDefault(),它将永远不会提交,也不会使用 $。ajax()发布数据

This is because you are not posting the data to the form url. As you have used event.preventDefault() which will never submit nor you used $.ajax() to post data

尝试使用ajax发布数据,

Try to post the data using ajax like,

$("#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);
       }
    });
});

在节点js中你会得到,

In node js you will get,

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
});

这篇关于如何将复选框值从HTML发送到Node JS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆