首页 > 解决方案 > 在ajax成功回调函数中输入type="file"无效的js点击事件

问题描述

如果不知道有没有遇到过这个问题,我一直在困扰这个问题。请问大家,这是为什么呢。

$('.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){

        }
    });

})

标签: javascriptajax

解决方案


@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"/>


推荐阅读