简体   繁体   English

jQuery .click()函数在ajax调用中不起作用

[英]JQuery .click() function not working inside ajax call

I have the following form with a file input which needs to be activated depending on the result of an Ajax Call. 我有以下形式的文件输入,需要根据Ajax调用的结果来激活它。

<form id="checkin_form" enctype="multipart/form-data" method="post" style="visibility:hidden;">
    <input type="file" name="file[]" id="checkinfile"><br>
</form>

The javascript function which will trigger the click funtion is: 触发点击功能的javascript函数为:

function clickCheckInFile() 
{
    var file_index = selected_file.id;
    var file_name = $(selected_file).children('td:nth(1)').text();

    $.ajax({
        url: "scripts/check_file.php",
        type: "POST",
        dataType:'json',
        data: {file_index: file_index, file_name: file_name},
        success: function(data){
        if (data['response'] == 200)
        {
            if (data['type'] != "dir")
            {
                if (data['checked_out'] == 1 || data['checked_out'] == "1")
                {
                    if (data['checked_out_by'] == username)
                    {
                        if (data['user_permissions'][1] == 1)
                        {
                            $('#checkinfile').click(); 
                        }
                        else
                        {
                            alert('Access Denied.');
                        }
                    }
                    else
                    {
                        alert('Access Denied');
                    }
                }
                else
                {
                    alert('File is not checked out.');
                }
            }
            else
            {
                alert('Cannot check out a folder');
            }
        }
        else if (data['response'] == 300)
        {
            alert(data['message']);
        }
        else
        {
            handleAjaxResponse(data['response']);
        }
    }});
}

The line not working is the $('#checkinfile').click(); 该行不起作用是$('#checkinfile')。click();。 portion. 一部分。 I can put an alert message that triggers in that spot so the code is calling that line. 我可以在该位置触发一条警报消息,以便代码调用该行。 When I move the click to prior to the ajax call it works fine. 当我将单击移动到ajax调用之前时,它工作正常。

It seems you're trying to force a click event. 看来您正在尝试强制执行click事件。 the .click() says what to do when it's clicked - it doesn't trigger a click. .click()表示单击时该怎么做-它不会触发点击。

Use .trigger() for that: 为此使用.trigger():

$('#checkinfile').trigger('click');

Then you need to separately add what to do when it's clicked like this: 然后,您需要单独添加单击时的操作,如下所示:

 $('#checkinfile').click(function(){
//do things here
});

As for the issue with the file browser . 至于文件浏览器问题 Try this solution from this article : 从本文尝试以下解决方案:

Position the input as position:absolute and top:*number that will remove it from viewport* and it'll work 将输入position:absoluteposition:absolutetop:*number that will remove it from viewport* ,它将起作用

Here's the docs for .trigger() 这是.trigger()的文档

Though it's generally preferred to use .trigger() instead when triggering an event (if for no other reason than clarity), this line of code seems to work fine: 虽然通常最好在触发事件时使用.trigger()代替 (如果出于清晰性没有其他原因),此代码行似乎正常工作:

$('#checkinfile').click();

(assuming that the selector finds the element(s) you expect, of course) (当然,假设选择器找到了您期望的元素)

However, the real question is... What do you expect this element to do when the click event is triggered? 但是,真正的问题是...触发click事件时,您希望此元素什么? According to the code posted, there is no event handler for that. 根据发布的代码,没有为此的事件处理程序。

It's a file input, so maybe you expect it to open the file dialog for the user? 这是文件输入,所以也许您希望它为用户打开文件对话框? While it's possible that some browsers may do that, I suspect it's not standard or expected behavior. 尽管某些浏览器可能会这样做,但我怀疑这不是标准行为或预期行为。 The file input is notorious for things like this (events, styling, etc.) and can be a bit of a pain at times. 文件输入因此类事件(事件,样式等)而臭名昭著,有时可能会有些痛苦。

What I suspect is happening is that the browser, potentially for security reasons, is requiring actual user interaction before invoking the file dialog. 怀疑正在发生的情况是,出于安全原因,浏览器在调用文件对话框之前需要实际的用户交互。 Again, this behavior may be browser specific, but take a look at a couple of examples here: 同样,此行为可能是特定于浏览器的,但请在此处查看几个示例:

The pattern seems to be that manual interaction is required to open the file dialog, even if it's opened from code. 模式似乎是,即使从代码中打开文件对话框,也需要手动交互。

Check this out: 看一下这个:

 $('#clickme').click(function() { $('#checkinfile').click(); }); $(document).on('change', '#checkinfile', function() { alert($(this).val()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form enctype="multipart/form-data" id="checkin_form" style="visibility:hidden;"> <input type="file" name="file" id="checkinfile"> </form> <button id="clickme">click</button> 

Its easy 这很容易

$( "example" ).on( "click", function() {
    alert( "click" );
});

You can see documentation here: http://api.jquery.com/on/ 您可以在此处查看文档: http : //api.jquery.com/on/

Hope it helps you 希望对您有帮助

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

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