如何使用jquery将用户重定向到特定页面而不是动作?

时间:2016-03-11 10:24:30

标签: jquery html5

我想在成功提交表单时将用户重定向到特定页面。

<form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST" id="newform">
    <!-- Form Fields -->
    <button type="submit" form="newform" value="Submit" class=" btn btn-primary btn-lg">
        Request Information 
        <i class="fa fa-chevron-right"></i>
    </button>
</form>

这里的条件是我希望将URL保留在action属性中,但是我希望将用户重定向到我想要的页面而不是action页面。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

假设SalesForce端点接受跨域请求,请向其发出$.post请求,然后在请求的回调中使用window.location.assign来更改页面位置。试试这个:

$('#newform').submit(function(e) {
    e.preventDefault();

    // show loading spinner here...

    $.post(this.action, $(this).serialize(), function() {
        window.location.assign('/otherpage.html');
    });
});

如果SalesForce端点不接受跨域请求,那么单凭JS就无法实现所需。