输入类型=“文件”无效js点击事件在ajax成功回调函数中

时间:2018-06-06 07:32:19

标签: javascript ajax

如果你不知道你是否遇到过这个问题,我一直困扰着这个问题。请大家,为什么会这样。

$('.bottom').click(function(){
    $('input').click();  // Put on the outside of the ajax can be performed
    $.ajax({
        type: 'post',
        url: url,
        data: data,
        dataType: 'json',
        contentType: 'application/json',
        success: function(data){
            $('input').click(); // This is invalid
        },
        error: function(data){

        }
    });

})

1 个答案:

答案 0 :(得分:0)

@ Rachel.C尝试以下解决方案

  $(document).ready(function(){
$('.bottom').click(function(){
   
    $.ajax({
        type: 'post',
        url: "https://reqres.in/api/users?page=2",
        dataType: 'json',
        contentType: 'application/json',
        crossdomain:true,
        async:false,
        success: function(data){
            alert("2");
            $('#iptFile').trigger('click'); // This is invalid
        },
        error: function(data){

        }
    });

})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>This is a heading</h2>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

<button class="bottom">Click me</button>

<input type="file" Id="iptFile"/>