Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ajax成功后如何按下按钮?_Javascript_Settimeout - Fatal编程技术网

Javascript Ajax成功后如何按下按钮?

Javascript Ajax成功后如何按下按钮?,javascript,settimeout,Javascript,Settimeout,当Ajax调用成功时,用户将被重定向到主页: $('#button_1').on('click', function () { //code $.ajax({ method: "POST", url: "/somewhere", data: { //code

当Ajax调用成功时,用户将被重定向到主页:

$('#button_1').on('click', function () {
                //code
                $.ajax({
                    method: "POST",
                    url: "/somewhere",
                    data: {
                        //code
                    }
                }).done(function () {
                        location.href = "/";
                });    
            });
$(document).ready(function () {
        var reload = sessionStorage.getItem("reload");
        if (reload == "true") {
            $("#button_2").click();
            sessionStorage.setItem("reload", false);
        }
    });
重定向后,我想自动单击另一个按钮,打开一个模式。我尝试了内部
done
函数不同的方法:

.done(function () {
    location.href = "/";
    $('#button_2').click(); // doesn't work 
 }); 

.done(function () {
    location.href = "/";
    setTimeout(function ()
        $('#button_2').click(); // doesn't execute  
    }, 2000);
 });

.done(function () {
    location.href = "/";
    $(document).ready( function () {
        // executes but user is immediately redirected to main page
        $('#button_2').click(); 
    });
 }); 
我也尝试过在Ajax之外,在Ajax调用所在的函数中,但得到了相同的结果


如何在Ajax调用后以编程方式单击按钮?

旁边的代码行

location.href = "/";
将不会执行,因为浏览器正在导航到另一页


因此,您应该将逻辑放在
/
页面中(
#按钮2
应该在该页面中)。

旁边的代码行

location.href = "/";
将不会执行,因为浏览器正在导航到另一页


因此,您应该将逻辑放在
/
页面中(
#按钮2
应该在该页面中)。

您需要在目标(主)页面中添加该逻辑

重定向后,当前页面不再是活动页面,所有逻辑都将被删除

您可以向URL添加一些参数,以便知道由于重定向,您何时在URL中,例如:

location.href="/?redirect=1"

然后检查该参数

您需要在目标(主)页面中添加该逻辑

重定向后,当前页面不再是活动页面,所有逻辑都将被删除

您可以向URL添加一些参数,以便知道由于重定向,您何时在URL中,例如:

location.href="/?redirect=1"

然后检查参数

您当前正试图在重定向后执行代码,这是不可能直接执行的,因为
location.href=“/”
后的代码将永远无法到达。一旦你重定向,你就有了一个状态清晰的新页面

您当前的函数仍然可以如下所示:

$('#button_1').on('click', function () {
    $.ajax({
        method: "POST",
        url: "/somewhere",
        data: {
            //code
        }
    }).done(function () {
            location.href = "/?modal-open=1";
    });    
});
$(document).ready( function () {
    // check the URL for the modal-open parameter we've just added.
    if(location.search.indexOf('modal-open=1')>=0) {
        $('#button_2').click(); 
    }
});
正如您所看到的,我已经向重定向添加了一个查询参数,因此我们知道重定向的目的是打开模式

对于根页面(
/
),您需要这样一个脚本:

$('#button_1').on('click', function () {
    $.ajax({
        method: "POST",
        url: "/somewhere",
        data: {
            //code
        }
    }).done(function () {
            location.href = "/?modal-open=1";
    });    
});
$(document).ready( function () {
    // check the URL for the modal-open parameter we've just added.
    if(location.search.indexOf('modal-open=1')>=0) {
        $('#button_2').click(); 
    }
});

这将检查参数是否在URL中,并触发单击。

您当前正试图在重定向后执行代码,而重定向无法直接执行,因为将永远无法到达
location.href=“/”
之后的代码。一旦你重定向,你就有了一个状态清晰的新页面

您当前的函数仍然可以如下所示:

$('#button_1').on('click', function () {
    $.ajax({
        method: "POST",
        url: "/somewhere",
        data: {
            //code
        }
    }).done(function () {
            location.href = "/?modal-open=1";
    });    
});
$(document).ready( function () {
    // check the URL for the modal-open parameter we've just added.
    if(location.search.indexOf('modal-open=1')>=0) {
        $('#button_2').click(); 
    }
});
正如您所看到的,我已经向重定向添加了一个查询参数,因此我们知道重定向的目的是打开模式

对于根页面(
/
),您需要这样一个脚本:

$('#button_1').on('click', function () {
    $.ajax({
        method: "POST",
        url: "/somewhere",
        data: {
            //code
        }
    }).done(function () {
            location.href = "/?modal-open=1";
    });    
});
$(document).ready( function () {
    // check the URL for the modal-open parameter we've just added.
    if(location.search.indexOf('modal-open=1')>=0) {
        $('#button_2').click(); 
    }
});

这将检查参数是否在URL中,并触发单击。

首先,当您重定向新页面时,您无法访问DOM,因为该页面已重新加载。您可以发送一个参数来执行此操作

$(document).ready( function () {

    //after page load

    var isButtonActive = findGetParameter("isButtonActive");
    if(isButtonActive){
        alert("button_2 activated");
        $('#button_2').click(); 
    }

    $('#button_1').on('click', function () {
        //code
        $.ajax({
            method: "POST",
            url: "/somewhere",
            data: {
                //code
            }
        }).done(function () {
            alert("page will be reload. button_2 wil be activated");
            location.href = "/?isButtonActive=true";
        });    
    });

    $('#button_2').on('click', function () {
        alert("button_2 clicked");
    });

});

function findGetParameter(parameterName) {
    var result = null,
        tmp = [];
    location.search
        .substr(1)
        .split("&")
        .forEach(function (item) {
          tmp = item.split("=");
          if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
        });
    return result;
}

首先,当您重定向一个新页面时,您无法访问DOM,因为该页面已重新加载。您可以发送一个参数来执行此操作

$(document).ready( function () {

    //after page load

    var isButtonActive = findGetParameter("isButtonActive");
    if(isButtonActive){
        alert("button_2 activated");
        $('#button_2').click(); 
    }

    $('#button_1').on('click', function () {
        //code
        $.ajax({
            method: "POST",
            url: "/somewhere",
            data: {
                //code
            }
        }).done(function () {
            alert("page will be reload. button_2 wil be activated");
            location.href = "/?isButtonActive=true";
        });    
    });

    $('#button_2').on('click', function () {
        alert("button_2 clicked");
    });

});

function findGetParameter(parameterName) {
    var result = null,
        tmp = [];
    location.search
        .substr(1)
        .split("&")
        .forEach(function (item) {
          tmp = item.split("=");
          if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
        });
    return result;
}

谢谢你的回答。我做了更多的研究,找到了另一种方法:

$('#button_1').on('click', function () {
                sessionStorage.setItem("reload", "true");
                //code
                $.ajax({
                    method: "POST",
                    url: "/somewhere",
                    data: {
                        //code
                    }
                }).done(function () {
                        location.reload();
                });    
            });
在主页中:

$('#button_1').on('click', function () {
                //code
                $.ajax({
                    method: "POST",
                    url: "/somewhere",
                    data: {
                        //code
                    }
                }).done(function () {
                        location.href = "/";
                });    
            });
$(document).ready(function () {
        var reload = sessionStorage.getItem("reload");
        if (reload == "true") {
            $("#button_2").click();
            sessionStorage.setItem("reload", false);
        }
    });

谢谢你的回答。我做了更多的研究,找到了另一种方法:

$('#button_1').on('click', function () {
                sessionStorage.setItem("reload", "true");
                //code
                $.ajax({
                    method: "POST",
                    url: "/somewhere",
                    data: {
                        //code
                    }
                }).done(function () {
                        location.reload();
                });    
            });
在主页中:

$('#button_1').on('click', function () {
                //code
                $.ajax({
                    method: "POST",
                    url: "/somewhere",
                    data: {
                        //code
                    }
                }).done(function () {
                        location.href = "/";
                });    
            });
$(document).ready(function () {
        var reload = sessionStorage.getItem("reload");
        if (reload == "true") {
            $("#button_2").click();
            sessionStorage.setItem("reload", false);
        }
    });

/
页面中的
$(“#按钮_2')
中尝试
成功:
属性?如果将用户重定向到另一页面,则无法在第一页中执行此操作。您必须在第二个(重定向)页面上执行此操作。您必须在目标页面(位于
/
)中放置该单击逻辑@Satpal是的,我尝试了,并
.trigger('click')
。它们可以工作,但用户会立即重定向到主页面。请在
/
页面中的
$(“#按钮_2”)
中尝试
成功:
属性。如果将用户重定向到另一个页面,则无法在第一个页面中执行此操作。您必须在第二个(重定向)页面上执行此操作。您必须在目标页面(位于
/
)中放置该单击逻辑@Satpal是的,我尝试了,并
.trigger('click')
。它们可以工作,但用户会立即重定向到主页。我这样做更容易处理,但是+1对于你的建议我这样做更容易处理,但是+1对于你的建议谢谢你,+1对于好的答案。我用我的答案解决了它,因为它更容易处理谢谢你,好的答案+1。我用我的答案解决了它,因为它更容易处理