doulu4203 2014-05-10 13:11
浏览 54
已采纳

使用内联javascript将html属性分配给php变量

I'm trying to use the id attribute of a html link as a php variable. I am trying to do this using javascript as below:

<a data-toggle="modal" data-target="#profileModal" id="<?php echo $id; ?>" onclick="<?php $profile_id='<script> function(){ var pid = $(this).attr(\"id\")}; document.write(pid);  }<\/script>';?>"><?php echo $first_name; ?></a>

I can document.write static text out of that function but there is no output when I add:

var id = $(this).attr(\"id\");

and try output that.

The links are on the first name in a table that will open a modal. How can I make this happen?

  • 写回答

1条回答 默认 最新

  • douweng3383 2014-05-10 13:15
    关注

    For inline event handlers, don't use <script> tags.

    onclick="var pid = $(this).attr(\"id\")}; document.write(pid);"
    

    Note document.write() overwrites the whole page content.

    Unless you're just calling a function, inline handlers get ugly, as your code shows, so consider using unobtrusive JavaScript to assign the event handlers.

    For example if you give the links a class:

    $(function(){
        $('.myClass').click(function(){
            console.log( this.id );
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功