怎么把result 的返回结果放在变量tickets里面 ?

0 0 javascript ajax jquery
狂奔岻蜗牛
狂奔岻蜗牛

声望值:162 0人

2019-02-18 14:55:50 提问

关注 0关注

收藏 0收藏, 125浏览

服务器返回的数据如何添加到 声明的变量里面? 如下代码:
$.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 = '\
                          
\
\ '+ result[i].TrainCode+'\
\
\

StartBeijing South

\

EndShanghai Hongqiao

\
\
\ 20:00\ 12:10\ +2\ \
\
\ 40h10m\
\
\

Business cabins ¥360\

\

First-class seats ¥720\

\

Second-class seats ¥1144\

\

Advanced soft sleeper ¥360

\
\
\

100张

\

100张

\

仅2张

\

 

\
\

\ '; 我把result放进去后 系统提示result不存在。 怎么把result 的返回结果传在变量tickets里面 ? 谢谢

请先 登录 后评论

1个回答

  • 兄弟快醒醒87声望 2018-09-25 09:26

    你这个要写在success里面的,可以写个函数把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="车次到达日期 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 ; }
    请先 登录 后评论

注册新账号

悬赏追问
10
  • 10
  • 20
  • 50
  • 100
  • 200
  • 输入数值
发布追问