dtwxmn8741 2014-11-02 18:12
浏览 53
已采纳

在JavaScript中访问多维PHP数组

I want to show a jQuery dialog with details of one row in the table (that only shows part of information). This is an example of what I want to do.

The problem: When the image of 'View more details' of a row is clicked, the JS function receives the id of the row clicked. I can't assign to JS variable 'data' as I did in the HTML part of the code. How can I solve this? Is a good way to do it?

<script>
    $(function() {    
        $( "#dialog" ).dialog({
            autoOpen: false,
            height: 400,
            width: 600,
            buttons: [{
                text: "Close",
                click: function() {
                    $(this).dialog("close");
                }
            }]
        });

        $(".details").click(function(){
            $("#dialog").empty();
            var i = $(this).attr('id');
            var data = <?=$cars[i]['price']?>;
            $("#dialog").append("<p>Car:"+data+"</p>");
            $("#dialog").dialog("open");
        });
    });
</script>

<table>
    <?php for($i = 0; $i < count($cars); $i++) { ?>
        <tr>
            <td><?=$cars[$i]['model']?></td>
            <td><?=$cars[$i]['colour']?></td>
            <td> 
                <img class="details" id="<?=$i?>" style="cursor: pointer;" src="http://localhost/ci/public/images/details.png" width="40" height="40" alt="Details"/>
            </td>
        </tr>
    <?php } ?>
</table>
  • 写回答

2条回答 默认 最新

  • dtgv52982 2014-11-02 18:31
    关注

    You're trying to use data that is on the server on the client without any code to send it to the client. You have to have a way to get the data from php to javascript. Here is one alternative, which uses HTML data attributes.

    In your image tag do this:

     <img class="details" id="<?=$i?>" data-price="<?=htmlentities($cars[$i]['price'])?>" style="cursor: pointer;" src="http://localhost/ci/public/images/details.png" width="40" height="40" alt="Details"/>
    

    This just saves some data in a form that the client can read easily. htmlentities is there just for safety in case the variable contains quote characters (intentionally or not)

    I should point out that there is no explicit need for the id attribute at this point unless you are using it for something else.

    Then you can change your click function to the following (from the top of my head, not tested, consult the documentation for .data()):

    $(".details").click(function(){
            $("#dialog").empty();
            var data = $(this).data('price');
            $("#dialog").append("<p>Car:"+data+"</p>");
            $("#dialog").dialog("open");
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名