如果ajax中的语句附加块

时间:2014-10-18 16:36:59

标签: javascript jquery ajax

我有一个ajax块正在处理来自外部API的json响应。我有一个名为status的对象字段,根据状态,我想显示相应的工具提示。目前,我试图使用if语句执行此操作,但我收到错误。

错误是:

Uncaught SyntaxError: Unexpected token if 

Ajax块

<script type="text/javascript">
  $.ajax({
    url: "pull_overview",
    dataType: "json",
    cache: false,
      success: function(data){
        if ( data == null ) {
          $('#ajax-loader').hide()
          $('#overviewlist tbody').append("<tr><td colspan='100'><h3><center>You have no recent transactions.</center></h3></td></tr>");
        } 
        else {
          $('#ajax-loader').hide()
          $.each(data, function(index, element) {
            $('#overviewlist tbody').append("<tr><td>"    + element.name + "</td><td>" 
                                                          + element.time + "</td><td>" 
                                                          + element.type + "</td><td>"
                                                          + element.change_in_balance + "</td><td>"
                                                          + element.method + "</td><td>"
                                                          + 
                                                          if (element.status == 'New Transaction') {
                                                            "<td><button type='button' class='tooltips btn-u btn-u-xs btn-u-purple' data-toggle='tooltip' data-placement='right' title='Your transaction is currently being processed'>Pending</button></td></tr>"
                                                          } 
          });
        }
    }
  });
</script>

2 个答案:

答案 0 :(得分:2)

如错误所示,您有 SyntaxError 。固定代码是:

$.ajax({
    url: "pull_overview",
    dataType: "json",
    cache: false,
    success: function (data) {
        if (data == null) {
            $('#ajax-loader').hide()
            $('#overviewlist tbody').append("<tr><td colspan='100'><h3><center>You have no recent transactions.</center></h3></td></tr>");
        } else {
            $('#ajax-loader').hide()
            $.each(data, function (index, element) {
                $('#overviewlist tbody').append("<tr><td>" + element.name + "</td><td>" + element.time + "</td><td>" + element.type + "</td><td>" + element.change_in_balance + "</td><td>" + element.method + "</td><td>" + element.status == 'New Transaction' ?
                    "<td><button type='button' class='tooltips btn-u btn-u-xs btn-u-purple' data-toggle='tooltip' data-placement='right' title='Your transaction is currently being processed'>Pending</button></td></tr>" : "");
            });
        }
    }
});

改变了什么?

  • 一些遗漏的括号
  • 您不能在字符串中包含if语句(例如"foo" + if(something) "bar")。请改用三元运算符:"foo" + something ? "bar" : ""

有用的资源:

答案 1 :(得分:2)

在上一次append调用中,不能在字符串连接的中间放置if语句。这样做

content = "<tr><td>" + element.name + "</td><td>" 
          + element.time + "</td><td>" 
          + element.type + "</td><td>"
          + element.change_in_balance + "</td><td>"
          + element.method + "</td><td>";
if (element.status == 'New Transaction')
    content += "<td><button type='button' class='tooltips btn-u btn-u-xs btn-u-purple' data-toggle='tooltip' data-placement='right' title='Your transaction is currently being processed'>Pending</button></td></tr>";
$('#overviewlist tbody').append(content);