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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器