鍍金池/ 問答/HTML/ 怎么把result 的返回結(jié)果放在變量tickets里面 ?

怎么把result 的返回結(jié)果放在變量tickets里面 ?

服務(wù)器返回的數(shù)據(jù)如何添加到 聲明的變量里面? 如下代碼:
$.ajax({

            type: "post",
            url: "./cgi/xxxxxxxxxx",
            dataType: "json",
            data: {FromStation: DepartureCity, ArriveStation: ArrivalCity, DepartureDate: DepartDate},
            success: function (result) {
                console.log(result);
                var resultLen = result.length;
                for (var i = 0; i < resultLen; i++) {
                    $(".lv-thead").after(tickets);
                }
            }
        });
        
        tickets 是我聲明的變量 是一段很大的html  是列表 li組件 
        var tickets = '<li class="lv-tbody" data-testdt="202">\
                          <div>\
                              <div class="td col1">\
                                  <h3 class="train">'+ result[i].TrainCode+'</h3>\
                              </div>\
                              <div class="td col2">\
                                  <p class="start"><i class="ico-start">Start</i><span>Beijing South</span></p>\
                                  <p class="end"><i class="ico-end">End</i><span>Shanghai Hongqiao</span></p>\
                              </div>\
                              <div class="td col2">\
                                  <time class="startime">20:00</time>\
                                  <time class="endtime daytime">12:10\
                                      <span title="車次到達(dá)日期 2017-12-23" class="i_2day">+2</span>\
                                  </time>\
                              </div>\
                              <div class="td col6">\
                                  <time class="duration">40h10m</time>\
                              </div>\
                              <div class="td col3" style="width: 214px">\
                                  <p class="ticketed">Business cabins&nbsp;<span class="price"><em class="g-rmb">¥</em>360</span>\
                                  </p>\
                                  <p class="ticketed">First-class seats&nbsp;<span class="price"><em class="g-rmb">¥</em>720</span>\
                                  </p>\
                                  <p class="ticketed">Second-class seats&nbsp;<span class="price"><em class="g-rmb">¥</em>1144</span>\
                                  </p>\
                                  <p class="ticketed">Advanced soft sleeper&nbsp;<span class="price"><em class="g-rmb">¥</em>360</span></p>\
                              </div>\
                              <div class="td col4">\
                                  <p class="surplus">100張</p>\
                                  <p class="surplus">100張</p>\
                                  <p class="surplus"><span style="color: #fc773c">僅2張</span></p>\
                                  <p class="surplus">&nbsp;</p>\
                              </div>\
                          </div>\
                      </li>';
                      
            我把result放進(jìn)去后 系統(tǒng)提示result不存在。  怎么把result 的返回結(jié)果傳在變量tickets里面 ?  謝謝
回答
編輯回答
心癌

你這個要寫在success里面的,可以寫個函數(shù)把result傳過去,比如

$.ajax({

            type: "post",
            url: "./cgi/xxxxxxxxxx",
            dataType: "json",
            data: {FromStation: DepartureCity, ArriveStation: ArrivalCity, DepartureDate: DepartDate},
            success: function (result) {
                console.log(result);
                var resultLen = result.length;
                for (var i = 0; i < resultLen; i++) {
                    $(".lv-thead").after(aaa(result));
                }             
            }
        });

function aaa(result){

var tickets = '<li class="lv-tbody" data-testdt="202">\
                          <div>\
                              <div class="td col1">\
                                  <h3 class="train">'+ result[i].TrainCode+'</h3>\
                              </div>\
                              <div class="td col2">\
                                  <p class="start"><i class="ico-start">Start</i><span>Beijing South</span></p>\
                                  <p class="end"><i class="ico-end">End</i><span>Shanghai Hongqiao</span></p>\
                              </div>\
                              <div class="td col2">\
                                  <time class="startime">20:00</time>\
                                  <time class="endtime daytime">12:10\
                                      <span title="車次到達(dá)日期 2017-12-23" class="i_2day">+2</span>\
                                  </time>\
                              </div>\
                              <div class="td col6">\
                                  <time class="duration">40h10m</time>\
                              </div>\
                              <div class="td col3" style="width: 214px">\
                                  <p class="ticketed">Business cabins&nbsp;<span class="price"><em class="g-rmb">¥</em>360</span>\
                                  </p>\
                                  <p class="ticketed">First-class seats&nbsp;<span class="price"><em class="g-rmb">¥</em>720</span>\
                                  </p>\
                                  <p class="ticketed">Second-class seats&nbsp;<span class="price"><em class="g-rmb">¥</em>1144</span>\
                                  </p>\
                                  <p class="ticketed">Advanced soft sleeper&nbsp;<span class="price"><em class="g-rmb">¥</em>360</span></p>\
                              </div>\
                              <div class="td col4">\
                                  <p class="surplus">100張</p>\
                                  <p class="surplus">100張</p>\
                                  <p class="surplus"><span style="color: #fc773c">僅2張</span></p>\
                                  <p class="surplus">&nbsp;</p>\
                              </div>\
                          </div>\
                      </li>';
                      return tickets ;
}
2018年3月3日 20:30