【问题标题】:Is it possible to insert php variables into your js code? [duplicate]是否可以将 php 变量插入到您的 js 代码中? [复制]
【发布时间】:2012-07-13 16:38:19
【问题描述】:

可能重复:
passing variables from php to javascript

我正在动态生成一个列表。我想让每一行悬停在鼠标悬停上并且可以点击链接。我希望链接传递行内容的 id。

基本上:

foreach ($courses as $cid=>cinfo){ 
  $univ = $cinfo['univ'];
  $desc = $cinfo['desc'];
  $size = $cinfo['size'];
  $start = $cinfo['start'];
  print "<div class='rc_desc' id='rc_desc$cid'>"."$desc<br/>"."<b>$univ</b><br/>".
        "<span>Number of students</span>: $size<br/>".
        "<span>Started at</span>: ".date('F d, Y',strtotime($start))."<br/>".
}

<script>
    $(function ()
    {
      $('#rc_desc$cid').hover(function ()
      {
        $(this).toggleClass('.tr');
      });
      $('#rc_desc$cid').click(function ()
      {
        $(location).attr('href','student.php?$cid');
      });
    });
  </script>

问题出在 js/jquery 中。我希望能够在单击时获取 $cid 并将其传递给 student.php 页面。上面的 php 代码可以工作,但 js 当然不会。我知道客户端与服务器端语言的基础。这个问题不需要讲课。我知道我不能完全做到这一点,但这是我最终想要发生的事情。关于如何简单地实现这一目标的任何想法?提前感谢我的朋友们!

【问题讨论】:

  • 下面的建议解决了第一个问题。第二个问题是该函数需要动态处理列表。目前我的 js 只适用于最后一项......有什么想法吗?

标签: php javascript jquery ajax


【解决方案1】:

是的,如果您在 PHP 代码中包含 &lt;script&gt; 部分,您可以执行类似于以下的操作:

<script>
    var foo = <?php echo $foo; ?>;
</script>

在您的情况下,您将查看以下代码结构:

<script>
    $(function () {
        $('#rc_desc<?php echo $cid ?>').hover(function () {
            $(this).toggleClass('.tr');
        });
        $('#rc_desc<?php echo $cid ?>').click(function () {
            $(location).attr('href', 'student.php?<?php echo $cid ?>');
        });
    });
</script>

之所以会这样,是因为尽管 Javascript 是在客户端运行的,但它在呈现在页面上之前首先在服务器端进行了处理。因此,它会将$cid 的所有必要实例替换为您包含的实例。

祝你好运!

编辑

<script>
    $(function () {
        $('.rc_desc').hover(function () {
            $(this).toggleClass('.tr ');
        });
        $('.rc_desc').click(function () {
            $(location).attr('href', 'student.php?' + $(this).attr('id').split('rc_desc')[1]);
        });
    });
</script>

【讨论】:

  • 确认!对于列表中的所有项目,它似乎并不是动态工作的。只有最后一个加载项似乎有效。
  • @campatsky 答案已更新,试试看。
【解决方案2】:

你可以这样做:

    $(location).attr('href','<?php echo $cid ?>');

javascript 代码不知道它来自 php,它显示为一个常量(文字)。

【讨论】:

    【解决方案3】:

    是的,这是可能的。你所要做的就是把你的&lt;?PHP echo $cid; ?&gt;放在你需要的地方

    <script>
    $(function ()
    {
      $('#rc_desc<?PHP echo $cid; ?>').hover(function ()
      {
        $(this).toggleClass('.tr');
      });
      $('#rc_desc$cid').click(function ()
      {
        $(location).attr('href','student.php?<?PHP echo $cid; ?>');
      });
    });
    

    这是可能的,因为当脚本被放入页面时,cid 已经被服务器上的字符串替换了。由于 PHP 是服务器驱动的,所以在它返回 html/script 之前,它就像你自己放入一样。

    【讨论】:

      【解决方案4】:

      几乎没有办法实际交流 PHP 和 JavaScript。但是,最好和最简单的方法是在属性中设置 ID。新的 HTML5 数据属性将是完美的。

      例如,有一个锚标记与

      <span data-id="22">some event</a>
      

      然后:

      $(this).attr('href','student.php?'+$(this).attr('data-id'));
      

      或者直接使用

      <?php echo $id; ?>
      

      如果不是外部JS文件

      【讨论】:

        【解决方案5】:

        试试这个代码

        foreach ($courses as $cid=>cinfo){ 
          $univ = $cinfo['univ'];
          $desc = $cinfo['desc'];
          $size = $cinfo['size'];
          $start = $cinfo['start'];
          print "<div class='rc_desc' data-id='$cid' id='rc_desc$cid'>"."$desc<br/>"."<b>$univ</b><br/>".
                "<span>Number of students</span>: $size<br/>".
                "<span>Started at</span>: ".date('F d, Y',strtotime($start))."<br/>".
        }
        
        <script>
            $(function ()
            {
              $('#rc_desc$cid').hover(function ()
              {
                $(this).toggleClass('.tr');
              });
              $('.rc_desc').click(function ()
              {
                $(location).attr('href','student.php?' + $(this).attr("data-id"));
        
               // if you want to redirect the page do this
               // window.location.href = 'student.php?' + $(this).attr("data-id");
              });
            });
          </script>
        

        【讨论】:

          【解决方案6】:

          我认为你的屏蔽应该是:

          <script>
              $(function ()
              {
              <?php foreach($courses as $cid=>cinfo) : ?>
          
                $('#rc_desc<?php echo $cid; ?>').hover(function ()
                {
                  $(this).toggleClass('.tr');
                });
                $('#rc_desc<?php echo $cid; ?>').click(function ()
                {
                  $(location).attr('href','student.php?<?php echo $cid; ?>');
                }); 
                ";?>
                <?php endforeach; ?>
              });
            </script>
          

          更新

          但你真的不需要这样做,你有

          "<div class='rc_desc' data-id='$cid' id='rc_desc$cid'>"."$desc<br/>"."<b>$univ</b><br/>".
          "<span>Number of students</span>: $size<br/>".
          "<span>Started at</span>: ".date('F d, Y',strtotime($start))."<br/>".
          

          你可以试试这个

          $(function (){
                $('.rc_desc').hover(function ()
                {
                  $(this).toggleClass('.tr');
                });
                $('.rc_desc').click(function ()
                {
                  attrId = $(this).attr("data-id");
                  $(location).attr('href','student.php?'+id);
                });
          });
          

          【讨论】:

            猜你喜欢
            • 2020-12-10
            • 2012-02-21
            • 2017-09-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多