[英]Comma separated string not working
problem 问题
I need the return value from xhr request to be like 1,2,3 我需要xhr请求的返回值像1,2,3
so that I can append comma separated value in textbox. 这样我就可以在文本框中附加逗号分隔的值。
I tried but its only appending like 1, only. 我试过了,但它只能像1那样追加。
input data: 输入数据:
9037566, 9037566,
9037579, 9037579,
9037577, 9037577,
9030724, 9030724,
9019686, 9019686,
but output data should be like for example 但是输出数据应该像例如
1,2,3,4,5 1,2,3,4,5
json data as input json数据作为输入
{ "keyword_search":
{"data":
[
{"JOB_NO":"9037566"},
{"JOB_NO":"9037579"},
{"JOB_NO":"9037577"},
{"JOB_NO":"9030724"},
{"JOB_NO":"9019686"}
]}}
js code i tried with 我尝试过的js代码
$.ajax({
url: root_url + 'mylogs/keyword',
cache: false,
type: "POST",
data: {'data': keyword},
dataType: 'json',
success: function (html) {
var keyword=html.keyword_search.data;
$.each(keyword, function (key, value){
console.log(value.JOB_NO.split(',')+',');
but when I tried to append $("#id").val(value.JOB_NO.split(',')+','); 但是当我尝试附加$(“#id”)。val(value.JOB_NO.split(',')+',');
it only appending like 它只会像
9037566,
i help is most appreciated. 我的帮助是最感激的。
You are calling console.log()
for each iteration of the loop, and therefore you will get a seperate output for each piece of data. 您正在为循环的每次迭代调用
console.log()
,因此您将为每个数据块获得单独的输出。
Change the following: 更改以下内容:
success: function (html) {
var keyword=html.keyword_search.data;
$.each(keyword, function (key, value){
console.log(value.JOB_NO.split(',')+',');
To: 至:
success: function (html) {
var keyword=html.keyword_search.data;
var jobNos = keyword.map(function(i){
return i.JOB_NO;
});
console.log(jobNos.join(","));
Simplified example here: https://jsfiddle.net/Lscewr1v/ 此处的简化示例: https : //jsfiddle.net/Lscewr1v/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.