JQuery的。点击()函数不工作Ajax调用内 [英] JQuery .click() function not working inside ajax call

查看:120
本文介绍了JQuery的。点击()函数不工作Ajax调用内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下形式与需要依赖于一个Ajax调用的结果将被激活文件输入。

I have the following form with a file input which needs to be activated depending on the result of an Ajax Call.

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

JavaScript函数将触发点击防水功能失效:

The javascript function which will trigger the click funtion is:

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']);
        }
    }});
}

不工作的线是$('#checkinfile)点击()。一部分。我可以把触发在这点所以code呼吁该行警告信息。当我将点击之前AJAX调用它工作正常。

The line not working is the $('#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.

推荐答案

虽然它通常preferred到使用 .trigger()而不是触发,当一个事件(如果没有其他原因,比清晰度),这条线的code似乎做工精细:

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:

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

(假设选择发现你期待,当然,元素(S))

(assuming that the selector finds the element(s) you expect, of course)

然而,真正的问题是......你想到这个元素的的时候,点击触发事件是什么?按照code贴,没有事件处理程序的。

However, the real question is... What do you expect this element to do when the click event is triggered? 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:

  • Unable to invoke file dialog automatically
  • Able to invoke file dialog from code after manual button click
  • Unable to invoke file dialog, using manual button with automatic click

该模式似乎是手动交互来打开文件对话框,即使它从code打开。

The pattern seems to be that manual interaction is required to open the file dialog, even if it's opened from code.

这篇关于JQuery的。点击()函数不工作Ajax调用内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆