繁体   English   中英

在jquery中,调用用户定义的函数在google chrome中有效,但在mozilla中则无效

[英]In jquery, calling user defined function works in google chrome, but not in mozilla

我试图在ajax调用后在jquery中调用用户定义的函数,

需要调用的函数:downloadCurrentKey()

我的要求是点击“generate_billing_key”后,函数downloadCurrentKey()应自动调用,当我点击标签时,此时也应该调用函数。

它在谷歌浏览器中运行良好,但在Mozilla Firefox中运行不佳。

以下是js代码,请指导我。

    $(document).on('click', '#generate_billing_key', function(){
    var url = $('#hdGenerateBillingKeyPair').val();


    $.post(url, {


    }, function (data) {

        var obj = JSON.parse(data);
        content="<label id='btn_download_billing_key'>
                      Click here to download key</label>";;

        $("#newKeyDiv").html(content);
        downloadCurrentKey();
    });
});


$(document).on('click', '#btn_download_billing_key', function(){
    downloadCurrentKey();
});

function downloadCurrentKey(){
    var url=$('#hdDownloadBillingKeyPath').val();
        my_form=document.createElement('FORM');
        my_form.name='myForm';
        my_form.method='POST';
        my_form.action=url;
        my_form.submit();
}

网址代码如下

/**
 * @Route("/downloadBillingKey",name="download_billing_key")
 * @Template()
 */
public function downloadBillingKeyAction(Request $request) {
    $file_name="key";
    $file_content="";

    header("Content-type: plain/text");
    header("Content-Disposition: attachment; filename=$file_name.txt");
    header("Pragma: no-cache");
    header("Expires: 0");
    echo $file_content;
    die;
}

谢谢。

通过这种方式,我达成了解决方案。

- 从js文件中删除函数downloadCurrentKey()

- 在twig文件中添加了一个表单(而不是调用上面的函数,我调用这个添加函数的提交事件)

- 控制器中的代码保持不变。

js的代码

 $(document).on('click', '#generate_billing_key', function(){
            var url = $('#hdGenerateBillingKeyPair').val();

            $.post(url, {

            }, function (data) {

            var obj = JSON.parse(data);
           content="<label id='btn_download_billing_key'>
                      Click here to download key</label>";;

                 $("#newKeyDiv").html(content);
                $("#downloadFile").submit();
            });
        });    
    });


$(document).on('click', '#btn_download_billing_key', function(){
    $("#downloadFile").submit();
});

树枝代码

<form method="post" action="{{ path('download_billing_key') }}" id="downloadFile" />
</form>

你没有发布你的所有代码不是吗?

我告诉我,因为我试图在这里模拟你的代码,它只在我这样做时起作用:

在你的例子中,你没有发布任何js代码。

var obj = JSON.parse(data); (如$("#newKeyDiv").html(content)仅在存在要解析的数据时出现。

在此行content="<label id='btn_download_billing_key'>Click here to download key</label>";; 有两个;

也许最重要的是,您可能应该在页面中附加表单。 尝试这个:

function downloadCurrentKey(){
    var url=$('#hdDownloadBillingKeyPath').val();

    // Get the element that will recive the form
    my_tag=document.getElementById("someID");

    my_form=document.createElement('FORM');

    // Append the form in the page before you post
    my_tag.appendChild(my_form); 

    my_form.name='myForm';
    my_form.method='POST';
    my_form.action=url;
    my_form.submit();
}

您的页面必须包含以下内容:

<p id="someID"></p>

无论如何,我希望它对你有所帮助。

暂无
暂无

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

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