这是我的 js 代码,它是网站的一部分:
var user = {
username: " ",
comment: " "
};
var array = [];
$(document).ready(function() {
$("#mybutton").click(function() {
user.comment = $("#comment").text();
user.username = $("#username").text();
array.push(user);
alert(array[0]);
user.empty();
});
});
我收到了测试我的代码的警报。 #comment 和 #username 都是我的 index.hbs 文件中两个文本区域的 id 标签。每次我按下按钮时,我都会收到带有 [object Object] 的警报。我不知道我的代码有什么问题,但我认为这是因为 user.username 和 user.comment 没有将实际的评论和用户名发送给对象用户。
最佳答案
使用.val()
代替.text()
例如
user.comment = $("#comment").val();
此外,对于您的警报,您必须这样做;
alert(array[0].username);
关于jquery - 如何使用 JQuery 将评论从评论部分推送到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53051643/