thinkphp与javascript数据交互

 

thinkphp查询出来的数组

控制器代码

$income = Db::name('table')->select();
$this->assign('income',$income);

javascript代码

var arr = {:json_encode($income)};
    var total = new Array();
    var date = new Array();
    
    for(i=0;i<arr.length;i++){
        total.push(arr[i].total);
        date.push(arr[i].months);
    }

arr数组

total数组

 date数组

thinkphp查找出来的数组进行遍历

{foreach $income as $v}
        <td class="total">{$v.total}</td>
    {/foreach}

划重点 标签要用class不能用id

<script>
        var arr = new Array();
        $(".total").each(function(){
            arr.push($(this).text());
        });
        console.log(arr);
    </script>

这样就可以将class为total的标签的值全部放到 arr数组中了
在控制台打印就可以看到arr数组中存的值了

转载:https://blog.csdn.net/jianchenn/article/details/106888914?tdsourcetag=s_pctim_aiomsg

人生得意须尽欢,莫使金樽空对月.
原文地址:https://www.cnblogs.com/luojie-/p/13261705.html