我是Nodejs的新成员,我表示要从输入字段向数据库添加多个值。
这是我的表格:
form(method='post',action='/form',enctype='multipart/form-data')
.form-check
label First Question
input.form-check-input(name='first', type='radio', id="radio1", value="3" data="option1")
label.form-check-label(for="radio1") 1
input.form-check-input(name='first', type='radio', id="radio2", value="10" data="option2")
label.form-check-label(for="radio2") 2
input.form-check-input(name='first', type='radio', id="radio3", value="5" data="option3")
label.form-check-label(for="radio3") 3
input.form-check-input(name='first', type='radio', id="radio4", value="7" data="option4")
label.form-check-label(for="radio4") 4
在路由器文件中,我可以使用req.body.first
成功捕获所选输入无线电的值,但是我也想获取数据信息,并将其连同值一起存储在数据库中,例如{{1 }}。
我该怎么做?
答案 0 :(得分:0)
您可以在提交之前将shared.project.dll.config
值添加到表单数据中。
index.pug
data
app.js
script(src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js')
form(action='/form',method='post')
label First Question
input.form-check-input(name='first', type='radio', id="radio1", value="3" data="option1")
label.form-check-label(for="radio1") 1
input.form-check-input(name='first', type='radio', id="radio2", value="10" data="option2")
label.form-check-label(for="radio2") 2
input.form-check-input(name='first', type='radio', id="radio3", value="5" data="option3")
label.form-check-label(for="radio3") 3
input.form-check-input(name='first', type='radio', id="radio4", value="7" data="option4")
label.form-check-label(for="radio4") 4
input(type='submit', value='Submit')
script.
$(document).ready(function () {
$('form').submit(function (e) {
var $selectedEle = $('input[name=first]:checked');
var data = {
data: $selectedEle.attr('data')
};
data[$selectedEle.attr('name')] = $selectedEle.attr("value");
$.post({
type: "POST",
url: '/form',
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (res) {
console.log('res from server', res);
}
});
return false;// prevent default form submit
});
})