doubeiji2602 2017-06-04 11:29
浏览 29
已采纳

如何将PHP放入javascript中

I am trying to add a else if statement in my Javascript code but so far it is not succesfull.

Here is the original script:

{ "targets": 4, "render": function ( data, type, full, meta ) { return  '+data+'; } }

+data+ returns a 0 or 1

I tried:

{ "targets": 4, "render": function ( data, type, full, meta ) { return  '<?php if ('+data+' == 0){ <font color=red> } else if ('+data+' == 1){ <font color=blue> } ?>'+data+'</font>'; } }

But when I run this I get a browser error.

Can someone help me with it?

Edit 1:

I am not trying to pass variables and data from PHP to JavaScript. I am trying to get data from Javascript and pass it to my PHP.

  • 写回答

1条回答 默认 最新

  • duanmao1872 2017-06-04 11:37
    关注

    That's not how PHP works. You have a simple check here, why don't you use javascript conditionals? Here's how you can do it with JS.

    var test = {
        "targets": 4,
        "render": function (data, type, full, meta) {
            return '<font color="'+ (data === 0 ? 'red' : 'blue') +'">'+data+'</font>';
        }
    };
    
    // Or
    
    var test = {
        "targets": 4,
        "render": function (data, type, full, meta) {
            if (data === 0) {
                return '<font color="red">'+data+'</font>';
            }
    
            return '<font color="blue">'+data+'</font>';
        }
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?