简体   繁体   English

我如何才能使模式窗口(颜色框)加载javascript(而不仅仅是HTML)?

[英]How can I make a modal window (colorbox) load javascript (and not just the html)?

I have POST button which loads a colorbox modal window, using this code: 我有使用以下代码加载颜色框模式窗口的POST按钮:

<link rel="stylesheet" href="colorbox2.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="jquery.colorbox.js"></script>
<script>


    $(document).ready(function(){
    $(".cboxFormButton").click(function(e){
    e.preventDefault();
    var $form = $(this).closest('form');
    $.colorbox({
      href: $form.attr('action'),
      transition:"elastic",
      opacity:0.5,
      data: {
                a: $form.find('input[name="a"]').val(),
                b: $form.find('input[name="b"]').val()
                }
        });

        return false;
    });
});
</script>
</head>
<body>


<form action="rrr2.php" method="POST" target="_blank" class="">
    <input  name="a" type="hidden" value="2"/>
     <input  name="b" type="hidden" value="2"/>
    <input type="submit" class="button cboxFormButton" value="Test">
</form>

I would need to insert a javascript tracking snippet into rrr2.php, so anytime that button was clicked, it would be tracked. 我需要在rrr2.php中插入一个javascript跟踪代码段,因此只要单击该按钮,就可以对其进行跟踪。 So I've decided to test whether javascript in general would function if loaded from that window, and simply put just for testing, another colorbox code in the rrr2.php file (same code as above more or less), but it doesn't work. 因此,我决定测试从该窗口加载的javascript通常是否可以正常运行,并且只是为了测试而将rrr2.php文件中的另一个colorbox代码(与上面的代码大致相同)进行测试,但是它没有工作。 seems like only the html is processed. 似乎只处理html。

Why does it happen, and how can I overcome this? 为什么会发生这种情况,我该如何克服呢?

EDIT: is this suppose to make it work? 编辑:这是要使其工作吗?

<script>


    $(document).ready(function(){
    $(".cboxFormButton").click(function(e){
    e.preventDefault();
    var $form = $(this).closest('form');
    $.colorbox({
      href: $form.attr('action'),
      transition:"elastic",
      opacity:0.5,
      data: {
                a: $form.find('input[name="a"]').val(),
                b: $form.find('input[name="b"]').val()
                }
        });

    $.get('rrr2.php');

        return false;
    });
});
</script>

Add a separate AJAX call to the click event. 将单独的AJAX调用添加到click事件。

$(".cboxFormButton").click(function(e){
    e.preventDefault();
    var $form = $(this).closest('form');

    $.colorbox({
      href: $form.attr('action'),
      transition:"elastic",
      opacity:0.5,
      data: {
        a: $form.find('input[name="a"]').val(),
        b: $form.find('input[name="b"]').val()
      }
    });

    // run simple get request to track click
    $.get('rrr2.php');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM