JSON字符串在html数据属性中不起作用


JSON string doesnt work in html data attribute

我有几个复选框,每个复选框都有数据路由。在悬停时,我首先提醒或任何值,我只得到一个字母或符号。这是代码。

           foreach ($this->coords as $index=>$value) {
            echo '<label class="checkbox">
             <input checked type="checkbox" id='.$i .'
             data-route="[';
                 foreach ($value as $idroute){
                     echo '&#34;Route' . $id . '&#34;,';
                     $id++;
                 }
            echo ' ]" ';
            echo "onclick='isChecked(this);'> " . $index;
            echo "</label>";
            $i++;
        }

以及警报的功能

    $('.checkbox').bind('mouseenter', function() {
    var Route = $('input', this).data('route');
    alert(Route[1]);
});

我做错了什么?谢谢

var Route是一个字符串,所以当你提醒Route[1]时,它会提醒字符串的第一个字符,所以你可以用<,>然后您可以通过索引访问路由的元素。

这是一个示例代码。。。

$('.checkbox').bind('mouseenter', function() {
    var Route = $('input', this).data('route').split(",");
    alert(Route[0]); // will alert the first route
});