dongyuying1507 2014-05-22 09:54
浏览 28
已采纳

使用JavaScript将PHP值传递到另一个页面时出错

I have an HTML which displays data from a database table. There'a checkbox field, which stores id of the user. And when I click on a row, that ID has to be passed to another page using a JavaScript function. But I have encountered a few errors. I don't know where I went wrong.

This is my Javascript function. It is used to make the table rows clickable. SO when I click on a row, the corresponding ID is passed to editgroup.php page.

<script>
    $('table.table tbody tr').click(function(event)
    {
       if(event.target.type === 'checkbox')
       {
            event.preventdefault()
       }
       else
       {
        window.location='edituser.php?val=<?php echo $id; ?>';
       }
    }); 
</script>

Below given is my table:

<table id="edituser" class="clickable"
    <thead>
        <th></th>
        <th>UserID</th>
        <th>Username</th>
    </thead>
    <tbody>
        <?php
            for($i=0; $i<$x; $i++)
            {
             $id= $val_array[$i]['id'];                                  
            $userName= $val_array[$i]['userName'];
        ?>
        <tr>
            <td class="text-center"><input type="checkbox" value="<?php echo $id;  ?>"></td>
            <td><?php echo $id;  ?></td>
            <td><?php echo $userName;  ?></td>
        </tr>
        <?php } ?>
    </tbody>
</table>
  • 写回答

3条回答 默认 最新

  • doushui20090526 2014-05-22 10:00
    关注

    you have to make some change

    First in your tr add id like this

    <?php
    for($i=0; $i<$x; $i++)
    {
     $id= $val_array[$i]['id'];  //data from database                                
    $userName= $val_array[$i]['userName']; //data from database  
    ?>
    <tr id="<?php echo $id;?>"> <!-- add id here -->
    <td ><input type="checkbox" value="<?php echo $id;  ?>"></td>
    <td><?php echo $id;  ?></td>
    <td><?php echo $userName;  ?></td>
    </tr>
    <?php } ?>
    

    now your JS

    <script>
                $('table.clickable tbody tr').click(function(event){
                    if(event.target.type === 'checkbox'){
                        event.preventdefault()
                    }else{
                        var val = $(this).attr('id');
                        window.location='edituser.php?val='+val;
                    }
                }); </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!