简体   繁体   English

Ajax调用成功后,jQuery调用无法正常工作

[英]JQuery call not working after Ajax call succeeds

I have an Ajax call that looks like this: 我有一个看起来像这样的Ajax呼叫:

$("#start-upload-btn").click(function(){
    $.ajax({
        type: "post",
        url: "",
        data: {
            newProjectName: $('#project-name').val(),
            csrfmiddlewaretoken: csrfToken
        },
        success: function(data){
            $("#file-upload").click();
        }
    })
});

Upon success I want to perform a click on the element with id #file-upload to launch the file selection dialogue, but putting the code in success function fails to work. 成功后,我想单击ID为#file-upload的元素以启动文件选择对话框,但是将代码放入成功功能将无法正常工作。 It works anywhere else. 它在其他任何地方都可以使用。 Is there something special about the scope of the Ajax success function? 关于Ajax成功功能的范围有什么特别之处吗? I really cannot figure this out. 我真的无法弄清楚。

Thanks 谢谢

There is nothing inherently problematic about issuing a click on any normal element (including a button) from an ajax success callback. 从ajax成功回调中发出对任何常规元素(包括按钮)的click都没有固有的问题。

The problem is that a file-input dialog is not a "normal element". 问题在于文件输入对话框不是“普通元素”。 It has some specific security limitations - one of which clearly limits your interaction with it. 它具有一些特定的安全限制-其中之一显然限制了您与其的交互。

This is demonstrated by the following fiddle: https://jsfiddle.net/qhfwobpz/ 以下小提琴对此进行了演示: https : //jsfiddle.net/qhfwobpz/

You'll see that issuing a click on the file-upload directly works without a problem. 您会看到在file-upload click就可以正常工作。 Doing it from an ajax callback yo'll see the callback is called, but the file dialog never shows. 通过ajax回调进行操作,您会看到调用了该回调,但是文件对话框从不显示。

This answer gives more detail as to the "why" and it boils down to you can open the dialog from an event issued by the user but not purely programatically. 该答案提供了有关“为什么”的更多详细信息,归结为您可以从用户发出的事件中打开对话框,但不能纯粹以编程方式打开。

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

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