如何使一个聪明的条件在JavaScript中工作?

时间:2018-04-24 09:48:24

标签: javascript php smarty

我的JavaScript中包含以下代码:

<script>
    {literal}
        // Wait for document to be ready before doing JS / Jquery magic
        $(document).ready(function() {
        // Check if the status is allowed to become changed | If so disable status select and force ticket closure
        if(
            {/literal}'{$ticket.lock_date}'{literal} != null
            &&
            {/literal}'{$currentDate}'{literal} > {/literal}'{$ticket.lock_date}'{literal}
        ) {
            $('#status').attr("disabled", true);
            $('.select2project').attr("disabled", true);
            $('.select2lead').attr("disabled", true);
            $.ajax({
                type: 'GET',
                url: '/modules/support/ticketAutoclose.php?id='+'{/literal}{$ticket.id}{literal}'
            });
        }

        //more code down here
   {/literal}
</script>

锁定日期我的数据库中的字段为NULL。当我访问该文档时,上面的代码是活动的,即使我专门在第一次检查时运行&#34;!= null&#34;。

任何人都知道我做错了什么?这真的让我陷入困境中。

2 个答案:

答案 0 :(得分:0)

Smarty是服务器端模板引擎 - 它在输出发送到浏览器之前工作。当你用JS添加它的语法关键字时 - 为时已晚。这绝不会被聪明的引擎所引导和解释。尝试将您的代码放到模板中,并在PHP中将一些标志传递给它。

答案 1 :(得分:0)

修正了它!它归结为JS将我的DB NULL值作为空字符串读取。

<script>
  {literal}
    // Wait for document to be ready before doing JS / Jquery magic
    $(document).ready(function() {

        // Check if the status is allowed to become changed | If so disable status select and force ticket closure
        var lock_date = '{/literal}{$ticket.lock_date}{literal}';
        var currDate = '{/literal}{$currentDate}{literal}';
        var ticketID = '{/literal}{$ticket.id}{literal}';

        if(lock_date !== null && lock_date !== '' && currDate > lock_date) {
            $('#status').attr("disabled", true);
            $('.select2project').attr("disabled", true);
            $('.select2lead').attr("disabled", true);
            $('.editBtnLink').hide();
            $('.editBtnSave').hide();
            $.ajax({
                type: 'GET',
                url: '/modules/support/ticketAutoclose.php?id='+ticketID
            });
        }

    // Some more code

  {/literal}
</script>