我目前正在构建一个非常简单的cms。因此,首先是一个文本框,旁边有一个“”按钮,当您按下它时,它会插入一个新创建的文本框。
因此,我希望能够从创建的每个文本框中获取所有这些值,并将其发送到服务器。
我试过查一查,但什么也没找到!请找人帮忙:)
到目前为止,这是我的代码
app.js
router.post('/addname', function(req, res) {
// connect to the results collection
var db = req.db;
var collection = db.get('names');
// Get form values.
var content = req.body.names;
// Submit to the DB
collection.insert({
"content": content
}, function (err, doc) {
if (err) {
// If it failed, return error
res.send("There was a problem adding the information to the database.");
console.log(err);
}
else {
// And forward to success page
res.redirect("/");
}
});
翡翠
form(action='addname' method="post")
div#mynames()
button#addmore(style="margin-left:-220px;" onClick="addtextbox") Add more
input#names(name='names' placeholder='@', type='text')
jquery
function addTexbox(){
$("#names").append('<br><input placeholder="@" type="text" style="border-radius: 15px; border: none;" />');
// contains all name input element
var allname = $("#names:input");
console.log(allTags[0].value);
}
到目前为止,如果按下按钮,它可以生成新的文本框。
谢谢!
发布于 2014-11-09 07:18:03
首先,它应该是input#names(name='names[0]' ...
,因为它只是数组names
的一个元素,它将以req.body.names
的形式传递给服务器。
form(action='addname' method="post")
div#mynames()
button#addmore(style="margin-left:-220px;" onClick="addtextbox") Add more
input#names(name='names[0]' placeholder='@', type='text')
然后,当您添加新输入时,只需使用递增的数组计数器添加它们即可。
var inputCounter = 1;
function addTexbox(){
$("#names").append('<br><input name="names['+ inputCounter++ +']">');
}
https://stackoverflow.com/questions/26773503
复制相似问题