简体   繁体   English

javascript ajax请求中的if ... else语句如何执行?

[英]How to perform if…else statement in javascript ajax request?

I have these code: 我有以下代码:

function ambil_coa(hal_aktif,scrolltop){
  if($('table#coa-list').length > 0){
    // alert('ada tablenya');
    $.ajax('http://'+host+path+'/ambil',{
      dataType:'json',
      type: 'POST',
      success: function(data){
        $('table#coa-list tbody tr').remove();
        $.each(data.record , function(index, element){
           $('table#coa-list').find('tbody').append(
                '<tr align="center">'+
                ' <td class="text-nowrap">'+element.no_urut+'</td>'+
                ' <td class="text-nowrap">'+element.customer_ID+'</td>'+
                '</tr>'               
            )
        });
      }

    });
  }
}

I want to add some conditional statement below. 我想在下面添加一些条件语句。

If element.customer_ID return 0 , echo "-" ; 如果element.customer_ID返回0 ,则echo "-" ;
else if the value not 0 echo "Customer Name" else如果值不为0,则echo "Customer Name"

How to write the conditional script to the append section? 如何将条件脚本写入append部分?

The simplest way will be using ternary operator: 最简单的方法是使用三元运算符:

$('table#coa-list').find('tbody').append(
    '<tr align="center">'+
    ' <td class="text-nowrap">'+element.no_urut+'</td>'+
    ' <td class="text-nowrap">'+(element.customer_ID == 0? '-' : 'Customer Name' )+'</td>'+
    '</tr>'               
)

If you want to use another element property, then: 如果要使用另一个元素属性,则:

$('table#coa-list').find('tbody').append(
    '<tr align="center">'+
    ' <td class="text-nowrap">'+element.no_urut+'</td>'+
    ' <td class="text-nowrap">'+(element.customer_ID == 0? '-' : element.customer_Name )+'</td>'+
    '</tr>'               
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM