$.ajax({
url:'/getArticles',
method:'GET',
}).done(function(articles){
var content = '';
articles.forEach(function(e){
var res = "<div class='article'>" +
"<h3>" + e.title + "</h3>" +
"<p>" + e.content + "</p><br>" +
"<button onclick=crud.remove(" + e._id + ")>Remove</button><br>" +
"</div>";
content += res;
});
$('#allarticles').append(content);
});
window.crud = (function(){
// Remove an article
function remove(id){
console.log(id);
}
我如何正确地在这里插入e._id以便它将文章的id放在这里?
当我点击这个按钮时,上面写着:
(索引):1未登录的SyntaxError:无效或意外令牌
发布于 2016-09-22 13:01:45
使用jQuery创建按钮时出现语法错误。你错过了id的单引号。还有你漏掉了一些牙套。
<button onclick=crud.remove(" + e._id + ")>Remove</button><br>将上面的行替换为
<button onclick=crud.remove('" + e._id + "')>Remove</button><br>我已经更正了你的代码:
$.ajax({
url : '/getArticles',
method : 'GET',
}).done(
function(articles) {
var content = '';
articles.forEach(function(e) {
var res = "<div class='article'>" + "<h3>" + e.title
+ "</h3>" + "<p>" + e.content + "</p><br>"
+ "<button onclick=crud.remove('" + e._id
+ "')>Remove</button><br>" + "</div>";
content += res;
});
$('#allarticles').append(content);
});
window.crud = (function() {
// Remove an article
function remove(id) {
console.log(id);
}
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
https://stackoverflow.com/questions/39639723
复制相似问题