douxiong3245 2014-02-13 17:16
浏览 99
已采纳

如何将jQuery [each]循环中的JavaScript存储到php变量中

I am [successfully] storing a snippet of jQuery inside a php variable, with values inside the snippet being populated by php script like so:

...//collect necessary variables

$script = "
  <script type='text/javascript'>
    (function($) {
      analytics.identity('" . $cid . "', {
        created: '" . $created . "',
        email: '" . $email . "',
        ...: '" . $whatever . "'
      });
    })(jQuery);
  </script>
";

return $script;

I can also [successfully] get the name attribute of all forms on the page like so:

 <script type='text/javascript'>
    (function($) {
      $('form').each(function() {
        var formname = $( this ).attr('name');
        if(formname !== undefined) {
          console.log(index + ':' + encodeURIComponent(formname));
        };
      });
    })(Jquery);
</script>

The problem I'm having (maybe obviously) is the lack of experience with javascript to know how to incorporate the two so my $script would look like so:

$script = "
  <script type='text/javascript'>
    (function($) {
      analytics.identity('" . $cid . "', {
        created: '" . $created . "',
        email: '" . $email . "',
        ...: '" . $whatever . "'
      });
      analytics.trackForm($('form[name="formname1"]'),'Form Submitted', {
        lead: formname
      });
      analytics.trackForm($('form[name="formname2"]'),'Form Submitted', {
        lead: formname
      });
      ...//(n) number of form names
    })(jQuery);
  </script>
";

Latest script added directly to the footer:

<script type="text/javascript">
  (function($) {
   $('form').each(function() {
    var formname = $(this).attr('name');
    if( formname !== undefined) {
      console.log( formname );
      var forms = $('form[name="' + formname + '"]');
      var trackforms = analytics.trackForm(forms, 'Submitted Optin Form', { leadmagnet: "'" + formname + '"' });
      return trackforms;
    }
   });
  })(jQuery);
</script>

Console.log outputs the one form currently on the page, and if I add another, it outputs that correctly also, but the rest of the code is simply written as is and I'm not getting it.

Thanks again.

  • 写回答

1条回答 默认 最新

  • dongxu3029 2014-02-13 17:34
    关注

    document.write(...) is adding the string to the document not to the script.

    You need to return the functions you want.

    $script = "
      <script type='text/javascript'>
        (function($) {
          analytics.identify('" . $ifs_id . "', {
            created: '" . $created . "',
            email: '" . $email . "',
            firstName: '" . $first_name . "',
            leadsource: '" . $lead_source ."'
          });
          $('form').each(function( index ) {
            var formname = $( this ).attr('name');
            if( formname !== undefined) {
              //console.log( index + ':' + formname );
              var forms = $('form[name=\"+formname+\"]);
              var trackform = analytics.trackForm(forms, 'Submitted Opt In Form', {
                 leadmagnet : $( this ).attr('name')
               });
              return trackform;
    
            }
          });
        })(jQuery);
       </script>
    ";
    
    return $script;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R