【问题标题】:how to change one button based on value如何根据值更改一个按钮
【发布时间】:2019-01-06 18:29:06
【问题描述】:

我想根据 if else 条件更改我的 ajax 按钮。就像状态为 1 一样,它只会显示活动按钮,否则只会显示非活动按钮。我有两个按钮。我找不到我实际放置 if else 条件的逻辑。我的代码是:

function showAllArticle(status = "") {

  //alert(status);
  $.ajax({
    //cache: false,
    //headers: { "cache-control": "no-cache" },
    type: 'ajax',
    method: 'post',
    data: {
      status: status
    },
    url: '<?php echo base_url() ?>Article/showAllUser',
    async: true,
    dataType: 'json',
    //cache: false,
    success: function(data) {

      var html = '';
      var i;
      for (i = 0; i < data.length; i++) {
        //(data[i].status);


        html += '<tr>' +
          '<td>' + data[i].id + '</td>' +
          '<td>' + data[i].username + '</td>' +
          '<td>' + data[i].email + '</td>' +
          '<td>' +
          // '{% if data[i].status == 1 %}'
          '<a href="javascript:;" class="btn btn-info item-active" data="' + data[i].id + '">Active</a>' +
          //@if(data[i].status == 1)

          //'{% else %}' 
          '<a href="javascript:;" class="btn btn-danger item-inactive" data="' + data[i].id + '">Inactive</a>' +
          //'{% endif %}' 
          /* '</td>' +
                   '<td>'+
                   '+@if(data[i].status == 1)+'
                   'tseting'+
                           '+@elseif(data[i].status == 0)+'
                           'debug'+

                           '+@endif+'


                   '</td>'+*/
          '</tr>';
      }
      $('#showdata').html(html);
    },
    error: function() {
      alert('Could not get Data from Database');
    }
  });
}

【问题讨论】:

  • 根据代码 cmets,看起来您确切地知道将条件放在哪里。您只需要使用 Javascript 而不是任何看起来是的模板语言来完成。

标签: javascript php html ajax codeigniter


【解决方案1】:

你必须基于JS应用if条件,而不是你使用的模板语言,因为JS是在用户端执行的。

你必须做类似的事情:

    html += '<tr>';
    html +=  '<td>' + data[i].id + '</td>';
    html +=  '<td>' + data[i].username + '</td>';
    html +=  '<td>' + data[i].email + '</td>';
    html +=  '<td>';
    if (data[i].status == 1) {
      html += '<a href="javascript:;" class="btn btn-info item-active" data="' + data[i].id + '">Active</a>';
    } else if(data[i].status == 12) {
      //...
    } else { 
      html += '<a href="javascript:;" class="btn btn-danger item-inactive" data="' + data[i].id + '">Inactive</a>' +
    } 
      /* '</td>' +
               '<td>'+
               '+@if(data[i].status == 1)+'
               'tseting'+
                       '+@elseif(data[i].status == 0)+'
                       'debug'+

                       '+@endif+'


               '</td>'+*/
     html += '</tr>';

【讨论】:

    猜你喜欢
    • 2020-09-07
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-02
    相关资源
    最近更新 更多