更改 AJAX 请求后的项目值。

huangapple go评论63阅读模式
英文:

Change the values of an item after an AJAX request

问题

我尝试添加一个 if 语句,如果 value = 1,则将 weekday_tinyint 更改为 "Sunday",但当我这样做时,似乎会破坏返回的对象。

以下的代码似乎可以正常工作。请告诉我如何最好地创建一个数组,然后处理数组,以便在列出它们之前对各个项目进行更改。

谢谢。

<div class="mypanel"></div>
index = 0;
day = 0;

$.getJSON('https://arizona-na.org/bmlt/main_server/client_interface/json/?switcher=GetSearchResults&amp;services=5', function(data) {
  while (index < data.length) {
    if (text === undefined) {
      var text = `${data[index].weekday_tinyint}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `${data[index].start_time}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `${data[index].meeting_name}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `(` + `${data[index].formats}` + `)` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` +
        `<a target="_blank" href="https://maps.google.com/maps?q=` + `${data[index].latitude}` + `,` + `${data[index].longitude}` + `">` + `${data[index].location_text}` + `&amp;nbsp;&amp;nbsp;` + `${data[index].location_info}` + `&amp;nbsp;&amp;nbsp;` + `${data[index].location_street}` + `,&amp;nbsp;&amp;nbsp;` + `${data[index].location_municipality}` + `</a><br>`;
    } else {
      var text = text + `${data[index].weekday_tinyint}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `${data[index].start_time}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `${data[index].meeting_name}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `(` + `${data[index].formats}` + `)` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` +
        `<a target="_blank" href="https://maps.google.com/maps?q=` + `${data[index].latitude}` + `,` + `${data[index].longitude}` + `">` + `${data[index].location_text}` + `&amp;nbsp;&amp;nbsp;` + `${data[index].location_info}` + `&amp;nbsp;&amp;nbsp;` + `${data[index].location_street}` + `,&amp;nbsp;&amp;nbsp;` + `${data[index].location_municipality}` + `</a><br>`;
    }
    index++;
  }
  $(".mypanel").html(text);
});

我尝试将脚本更改为以下内容,并收到错误...

var index = 0;
var daytext = "";

$.getJSON('https://arizona-na.org/bmlt/main_server/client_interface/json/?switcher=GetSearchResults&amp;services=5', function(data) {

 while (index < data.length) {
    if (${data[index].weekday_tinyint} === 1) 
    daytext = `Sunday`; 
    else daytext = `other day`;
    
  if (text === undefined) {
   var text = `$daytext` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `${data[index].start_time}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `${data[index].meeting_name}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `(` + `${data[index].formats}` + `)` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + 
  `<a target="_blank" href="https://maps.google.com/maps?q=` + `${data[index].latitude}` + `,` +  `${data[index].longitude}` + `">` + `${data[index].location_text}` + `&amp;nbsp;&amp;nbsp;` +  `${data[index].location_info}` + `&amp;nbsp;&amp;nbsp;` + `${data[index].location_street}` + `,&amp;nbsp;&amp;nbsp;` + `${data[index].location_municipality}` + `</a><br>`;
  } else {
  var text = text + `${data[index].weekday_tinyint}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `${data[index].start_time}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `${data[index].meeting_name}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `(` + `${data[index].formats}` + `)` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + 
  `<a target="_blank" href="https://maps.google.com/maps?q=` + `${data[index].latitude}` + `,` +  `${data[index].longitude}` + `">` + `${data[index].location_text}` + `&amp;nbsp;&amp;nbsp;` +  `${data[index].location_info}` + `&amp;nbsp;&amp;nbsp;` + `${data[index].location_street}` + `,&amp;nbsp;&amp;nbsp;` + `${data[index].location_municipality}` + `</a><br>`;
  }
  index++;
 }
 $(".mypanel").html(text);
});
英文:

I have tried to add an if statement to change the weekday_tinyint to &quot;Sunday&quot; if value = 1, but when I do this it seems to mess up the returned object.

The following code seems to work just fine. Please advise me on the best way to create an array and then process the array so I can make changes to the individual items before listing them.

Thank you.

<!-- language: lang-html -->

&lt;div class=&quot;mypanel&quot;&gt;&lt;/div&gt;

<!-- language: lang-js -->

index = 0;
day = 0;
$.getJSON(&#39;https://arizona-na.org/bmlt/main_server/client_interface/json/?switcher=GetSearchResults&amp;services=5&#39;, function(data) {
while (index &lt; data.length) {
if (text === undefined) {
var text = `${data[index].weekday_tinyint}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `${data[index].start_time}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `${data[index].meeting_name}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `(` + `${data[index].formats}` + `)` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` +
`&lt;a target=&quot;_blank&quot; href=&quot;https://maps.google.com/maps?q=` + `${data[index].latitude}` + `,` + `${data[index].longitude}` + `&quot;&gt;` + `${data[index].location_text}` + `&amp;nbsp;&amp;nbsp;` + `${data[index].location_info}` + `&amp;nbsp;&amp;nbsp;` + `${data[index].location_street}` + `,&amp;nbsp;&amp;nbsp;` + `${data[index].location_municipality}` + `&lt;/a&gt;&lt;br&gt;`;
} else {
var text = text + `${data[index].weekday_tinyint}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `${data[index].start_time}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `${data[index].meeting_name}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `(` + `${data[index].formats}` + `)` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` +
`&lt;a target=&quot;_blank&quot; href=&quot;https://maps.google.com/maps?q=` + `${data[index].latitude}` + `,` + `${data[index].longitude}` + `&quot;&gt;` + `${data[index].location_text}` + `&amp;nbsp;&amp;nbsp;` + `${data[index].location_info}` + `&amp;nbsp;&amp;nbsp;` + `${data[index].location_street}` + `,&amp;nbsp;&amp;nbsp;` + `${data[index].location_municipality}` + `&lt;/a&gt;&lt;br&gt;`;
}
index++;
}
$(&quot;.mypanel&quot;).html(text);
});

I tried changing the script to the following and received errors...

var index = 0;
var daytext = &quot;&quot;;
$.getJSON(&#39;https://arizona-na.org/bmlt/main_server/client_interface/json/?switcher=GetSearchResults&amp;services=5&#39;, function(data) {
while (index &lt; data.length) {
if (${data[index].weekday_tinyint} === 1) 
daytext = `Sunday`; 
else daytext = `other day`;
if (text === undefined) {
var text = `$daytext` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `${data[index].start_time}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `${data[index].meeting_name}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `(` + `${data[index].formats}` + `)` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + 
`&lt;a target=&quot;_blank&quot; href=&quot;https://maps.google.com/maps?q=` + `${data[index].latitude}` + `,` +  `${data[index].longitude}` + `&quot;&gt;` + `${data[index].location_text}` + `&amp;nbsp;&amp;nbsp;` +  `${data[index].location_info}` + `&amp;nbsp;&amp;nbsp;` + `${data[index].location_street}` + `,&amp;nbsp;&amp;nbsp;` + `${data[index].location_municipality}` + `&lt;/a&gt;&lt;br&gt;`;
} else {
var text = text + `${data[index].weekday_tinyint}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `${data[index].start_time}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `${data[index].meeting_name}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + `(` + `${data[index].formats}` + `)` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + 
`&lt;a target=&quot;_blank&quot; href=&quot;https://maps.google.com/maps?q=` + `${data[index].latitude}` + `,` +  `${data[index].longitude}` + `&quot;&gt;` + `${data[index].location_text}` + `&amp;nbsp;&amp;nbsp;` +  `${data[index].location_info}` + `&amp;nbsp;&amp;nbsp;` + `${data[index].location_street}` + `,&amp;nbsp;&amp;nbsp;` + `${data[index].location_municipality}` + `&lt;/a&gt;&lt;br&gt;`;
}
index++;
}
$(&quot;.mypanel&quot;).html(text);
});

答案1

得分: 1

你正在做的事情看起来有点复杂。我建议简化整个过程,使用.map()函数而不是你的while循环,同时建议给你的HTML元素一个ID而不是类:

<div id="mypanel"></div>

然后你的脚本会是这样的:

$.getJSON('https://arizona-na.org/bmlt/main_server/client_interface/json/?switcher=GetSearchResults&services=5', function(data) {
  var text = data.map(obj => {
    var strDay = "";
    switch (parseInt(obj.weekday_tinyint)) {
      case 1: strDay = "Sunday"; break;
      case 2: strDay = "Monday"; break;
      case 3: strDay = "Tuesday"; break;
      case 4: strDay = "Wednesday"; break;
      case 5: strDay = "Thursday"; break;
      case 6: strDay = "Friday"; break;
      case 7: strDay = "Saturday"; break;
    }

    return `${strDay}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + 
      `${obj.start_time}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + 
      `${obj.meeting_name}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + 
      `(` + `${obj.formats}` + `)` + 
      `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` +
      `<a target="_blank" href="https://maps.google.com/maps?q=` + `${obj.latitude}` + 
      `,` + `${obj.longitude}` + `">` + `${obj.location_text}` + `&nbsp;&nbsp;` + 
      `${obj.location_info}` + `&nbsp;&nbsp;` + `${obj.location_street}` + 
      `,&nbsp;&nbsp;` + `${obj.location_municipality}` + `</a><br>`;
  });
  $("#mypanel").html(text);
});

这是JavaScript map() 函数的文档:https://www.w3schools.com/jsref/jsref_map.asp

这里是可运行的 JsFiddle:JsFiddle

英文:

It all looks a bit complex what you're doing there. I would suggest to simplify the whole thing and use .map() function instead of your while loop, also I suggest to give your html element an ID instead of the class:

&lt;div id=&quot;mypanel&quot;&gt;&lt;/div&gt;

then your script would be as follows

$.getJSON(&#39;https://arizona-na.org/bmlt/main_server/client_interface/json/?switcher=GetSearchResults&amp;services=5&#39;, function(data) {
var text = data.map(obj =&gt; {
var strDay = &quot;&quot;;
switch (parseInt(obj.weekday_tinyint)){
case 1: strDay = &quot;Sunday&quot;; break;
case 2: strDay = &quot;Monday&quot;; break;
case 3: strDay = &quot;Tuesday&quot;; break;
case 4: strDay = &quot;Wednesday&quot;; break;
case 5: strDay = &quot;Thursday&quot;; break;
case 6: strDay = &quot;Friday&quot;; break;
case 7: strDay = &quot;Saturday&quot;; break;
}
return `${strDay}` +`&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + 
`${obj.start_time}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + 
`${data[index].meeting_name}` + `&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` + 
`(` + `${obj.formats}` + `)` + 
`&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;` +
`&lt;a target=&quot;_blank&quot; href=&quot;https://maps.google.com/maps?q=` + `${obj.latitude}` + 
`,` + `${obj.longitude}` + `&quot;&gt;` + `${obj.location_text}` + `&amp;nbsp;&amp;nbsp;` + 
`${obj.location_info}` + `&amp;nbsp;&amp;nbsp;` + `${obj.location_street}` + 
`,&amp;nbsp;&amp;nbsp;` + `${obj.location_municipality}` + `&lt;/a&gt;&lt;br&gt;`;
});
$(&quot;#mypanel&quot;).html(text);
});

Here is the documentation for the JavaScript map() function
https://www.w3schools.com/jsref/jsref_map.asp

And here is the functioning JsFiddle

huangapple
  • 本文由 发表于 2020年1月4日 01:06:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定