过滤特定的复选框

时间:2018-05-18 11:01:21

标签: javascript node.js express

我有这段代码:

<form method="post">
    <% for(let i = 0; i < websites.length; i++){ let website = websites[i]; %>
    <fieldset id="site<%= i %>" style="padding-bottom: 0;
                padding-top: 10px;
                margin-bottom: 0;">
        <label for="checkbox<%= i %>">Attach BuiltWith results (cost 1 credit)</label>
        <input type="checkbox" name="checkbox<%= i %>" id="checkbox<%= i %>">
        <p style="color: <% if(errorMsgs[i] === 'Wrong website address'){ %> red;<% } else { %> green;<% } %>
                padding-bottom: 0;
                padding-top: 0;
                margin-bottom: 0;"
           class="">
            <%= errorMsgs[i] %>
        </p>
        <div class="">
            <input class="website"
                   name="website<%= i %>"
                   id="<%= i %>"
                   value="<%= website %>"
                   type="text"/>
            <a href="/websites/edit/<%= i %>"
               class="btn btn-info hide">
                Edit
            </a>
            <a href="/websites/delete/<%= i %>"
               data-id="<%= i %>"
               class="btn btn-danger confirmation removeBtn">
                Remove
            </a>
        </div>
    </fieldset>
    <% } %>
    <input type="submit"
           id="generateReport"
           class="btn btn-primary col-sm-offset-2"
           value="Generate a report"
           name="generateReport"
           data-toggle="modal"
           data-target="#exampleModal">
    <!-- Save button created using Javascript. In case JS is disabled -->
</form>

<%%>EJS templating system。这就是我在后端所做的事情:

router.post('/', (req, res, next) => {
    var checkboxes = Object.keys(req.body)
        .filter(key => key.startsWith('checkbox'))
        .map(k => req.body[k]);
    console.log(checkboxes);

    res.redirect('/reports');
});

我想要实现的目标:如您所见,可能会有很多网站被发送到后端。我需要在每个复选框上附上一个复选框。不幸的是,我现在得到的是:

[ 'on' ]

这不是我想要的。

我基本上需要知道具体检查了哪些复选框。我该怎么办?

编辑:这是req.body outprint

{ checkbox0: 'on',
    website0: 'google.com',
    website1: 'stackoverflow.com' }

这对我有用:

router.post('/', (req, res, next) => {
    var bwIndexes = [];
    var checkboxes = Object.keys(req.body)
        .filter(key => key.startsWith('checkbox'))
        .map(k => bwIndexes.push(k.slice(-1)));
    console.log(bwIndexes);
    console.log(req.body);

    res.redirect('/reports');
});

1 个答案:

答案 0 :(得分:1)

如果您的索引介于0到9之间

,将会有效
router.post('/', (req, res, next) => {
    var bwIndexes = [];
    var checkboxes = Object.keys(req.body)
        .filter(key => key.startsWith('checkbox'))
        .map(k => bwIndexes.push(k.slice(-1)));
    console.log(bwIndexes);
    console.log(req.body);

    res.redirect('/reports');
});

以下工作甚至索引超过9

router.post('/', (req, res, next) => {
    var bwIndexes = [];
    var checkboxes = Object.keys(req.body)
        .filter(key => key.startsWith('checkbox'))
        .map(k => bwIndexes.push(k.replace( /^\D+/g, '')));
    console.log(bwIndexes);
    console.log(req.body);

    res.redirect('/reports');
});