Javascript代码不起作用。它不会在重新加载时更新变量

时间:2014-04-13 08:19:01

标签: javascript php codeigniter variables

<div id="timelapse_speed_control">
    <?php
    $speed_var;
    $timeout_var;
    if ($this->uri->segment(4) == 1)
    {
        $speed_var = 200;
        $timeout_var = 200;
    }
    else if ($this->uri->segment(4) == 2)
    {
        $speed_var = 200;
        $timeout_var = 150;
    }
    else
    {
        $speed_var = 200;
        $timeout_var = 75;
    }
    ?>
    <script type="text/javascript">
        $(document).ready(
        function(){
            speed_var = <?php json_encode($speed_var); ?>;
            timeout_var = <?php json_encode($timeout_var); ?>;



            $('ul.<?php echo $uri_segment; ?>').innerfade({
                speed: speed_var,
                timeout: timeout_var,
                type: 'sequence',
                containerheight: '500px'
            });
        });
    </script>
</div>

上面的代码基本上是询问uri中的第4段是什么,然后检查它,并更改“游戏中时光倒流”的速度和超时时间。因此。但它不起作用。它在测试时崩溃了。

对此中的错误有任何想法吗?也许我做错了?

浏览器输出:

        $(document).ready(
        function(){
            speed_var = ;
            timeout_var = ;



            $('ul.jeromiedevera').innerfade({
                speed: speed_var,
                timeout: timeout_var,
                type: 'sequence',
                containerheight: '500px'
            });
        });

1 个答案:

答案 0 :(得分:1)

要解决此问题,请在echo之前添加json_encode

    <script type="text/javascript">
        $(document).ready(
        function(){
            speed_var = <?php echo json_encode($speed_var); ?>;
            timeout_var = <?php echo json_encode($timeout_var); ?>;

            $('ul.<?php echo $uri_segment; ?>').innerfade({
                speed: speed_var,
                timeout: timeout_var,
                type: 'sequence',
                containerheight: '500px'
            });
        });
    </script>