Switch 或 if elseif 语句成功:function(data) ajax

$status = $row->status;  

switch ($status){

case "1":

$status = '<span class="badge badge-danger">Open</span> ';

break;

case "2":

$status=  '<span class="badge badge-secondary">On Hold</span> ';

break;

case "3":

$status= '<span class="badge badge-success">In Progress</span> ';

break;

case "4":

$status= '<span class="badge badge-info">Closed</span> ';

break;

}

这是我的 php 代码中的 switch 语句;我如何做同样的现场 ajax 成功:函数(数据)


这是我的ajax函数


success: function(response){

      //console.log(response);

      $('.sub').html(response.sub);

      $('.msg').html(response.msg);

     //value = $(this).val(response.status); //Get the value from db;

     // if elseif or swithch statetment here//

      $('.sts').html('<span class="badge badge-danger">Open</span>'); //post it to html div;

    }

可以做什么??有什么办法可以在这里做或者我必须在编码json之前做吗?


明月笑刀无情
浏览 80回答 1
1回答

qq_花开花谢_0

您也可以像在 php 代码中使用它一样使用 switch case,如下所示:&nbsp; &nbsp; success: function(response){&nbsp; &nbsp; &nbsp; //console.log(response);&nbsp; &nbsp; &nbsp;$('.sub').html(response.sub);&nbsp; &nbsp; &nbsp;$('.msg').html(response.msg);&nbsp; &nbsp; &nbsp;var status = response.status; //Get the value from db;&nbsp; &nbsp; // if elseif or swithch statetment here//&nbsp; &nbsp; switch (status) {&nbsp; &nbsp; &nbsp; &nbsp; case "1":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('.sts').html('<span class="badge badge-danger">Open</span>');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "2":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('.sts').html('<span class="badge badge-secondary">On Hold</span>');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case "3":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('.sts').html('<span class="badge badge-success">In Progress</span>');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case "4":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('.sts').html('<span class="badge badge-info">Closed</span> ');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('.sts').html('<span class="badge badge-danger">Open</span>');&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP