我通过getJSON获取此数据,该数据将此数据返回到名为results的变量。
以下是数据:
{
"ok": true,
"messages": [
{
"text": "Message 1",
"username": "bot",
"type": "message",
"subtype": "bot_message",
"ts": "1454394770.000002"
},
{
"text": "Message 2",
"username": "bot",
"type": "message",
"subtype": "bot_message",
"ts": "1447656207.000002"
},
{
"user": "5434543",
"type": "message",
"subtype": "ch_join",
"text": "<@U0456TA8|tre> has joined",
"ts":654565.444"
}
],
"has_more": false
}
我知道如何将其中一个添加到我的textarea:
$("#myTextarea").val(results.messages[0].text);
我的问题是......如何将所有内容添加到我的textarea?
答案 0 :(得分:2)
var output = "";
jQuery.each(results.messages , function (index, element){
output += element.text + "something to put between the text perhaps?";
});
$("#myTextarea").val(output);
答案 1 :(得分:1)
你可以循环:
var _val = $("#myTextarea").val(); // Fetching the new value and adding it to previous value
[].forEach.call(results.messages, function (inst) {
_val += inst.text;
});
$("#myTextarea").val(_val); //setting it as a new value of textarea.